Move a bit more logic out of Module

This commit is contained in:
bjorn3
2020-09-30 17:31:08 +02:00
parent c2ffcdc6d0
commit 7a6e909efe
8 changed files with 66 additions and 53 deletions

2
Cargo.lock generated
View File

@@ -489,6 +489,7 @@ dependencies = [
"anyhow",
"cranelift-codegen",
"cranelift-module",
"log",
"object 0.21.1",
"target-lexicon",
]
@@ -535,6 +536,7 @@ dependencies = [
"cranelift-native",
"errno",
"libc",
"log",
"memmap",
"region",
"target-lexicon",

View File

@@ -1,19 +1,20 @@
//! Defines the `Backend` trait.
use crate::module::ModuleCompiledFunction;
use crate::DataContext;
use crate::DataId;
use crate::FuncId;
use crate::Linkage;
use crate::ModuleDeclarations;
use crate::ModuleResult;
use crate::DataContext;
use core::marker;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::Context;
use cranelift_codegen::{binemit, ir};
use std::borrow::ToOwned;
use std::boxed::Box;
use std::string::String;
use std::borrow::ToOwned;
/// A `Backend` implements the functionality needed to support a `Module`.
///
@@ -63,11 +64,10 @@ where
fn define_function<TS>(
&mut self,
id: FuncId,
ctx: &Context,
ctx: &mut Context,
declarations: &ModuleDeclarations,
code_size: u32,
trap_sink: &mut TS,
) -> ModuleResult<()>
) -> ModuleResult<ModuleCompiledFunction>
where
TS: binemit::TrapSink;
@@ -79,7 +79,7 @@ where
id: FuncId,
bytes: &[u8],
declarations: &ModuleDeclarations,
) -> ModuleResult<()>;
) -> ModuleResult<ModuleCompiledFunction>;
/// Define a zero-initialized data object of the given size.
///

View File

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

View File

@@ -8,12 +8,10 @@
use super::HashMap;
use crate::data_context::DataContext;
use crate::Backend;
use cranelift_codegen::binemit::{self, CodeInfo};
use cranelift_codegen::binemit;
use cranelift_codegen::entity::{entity_impl, PrimaryMap};
use cranelift_codegen::{ir, isa, CodegenError, Context};
use log::info;
use std::borrow::ToOwned;
use std::convert::TryInto;
use std::string::String;
use thiserror::Error;
@@ -333,7 +331,9 @@ where
backend: B,
}
/// Information about the compiled function.
pub struct ModuleCompiledFunction {
/// The size of the compiled function.
pub size: binemit::CodeOffset,
}
@@ -488,17 +488,8 @@ where
where
TS: binemit::TrapSink,
{
info!(
"defining function {}: {}",
func,
ctx.func.display(self.backend.isa())
);
let CodeInfo { total_size, .. } = ctx.compile(self.backend.isa())?;
self.backend
.define_function(func, ctx, &self.declarations, total_size, trap_sink)?;
Ok(ModuleCompiledFunction { size: total_size })
.define_function(func, ctx, &self.declarations, trap_sink)
}
/// Define a function, taking the function body from the given `bytes`.
@@ -513,27 +504,13 @@ where
func: FuncId,
bytes: &[u8],
) -> 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
.define_function_bytes(func, bytes, &self.declarations)?;
Ok(ModuleCompiledFunction { size: total_size })
.define_function_bytes(func, bytes, &self.declarations)
}
/// Define a data object, producing the data contents from the given `DataContext`.
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
self.backend.define_data(
data,
data_ctx,
&self.declarations,
)
self.backend.define_data(data, data_ctx, &self.declarations)
}
/// Return the target isa

View File

@@ -15,6 +15,7 @@ cranelift-codegen = { path = "../codegen", version = "0.67.0", default-features
object = { version = "0.21.1", default-features = false, features = ["write"] }
target-lexicon = "0.11"
anyhow = "1.0"
log = { version = "0.4.6", default-features = false }
[badges]
maintenance = { status = "experimental" }

View File

@@ -2,15 +2,16 @@
use anyhow::anyhow;
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::isa::TargetIsa;
use cranelift_codegen::{self, ir};
use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleDeclarations,
ModuleError, ModuleResult,
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleCompiledFunction,
ModuleDeclarations, ModuleError, ModuleResult,
};
use log::info;
use object::write::{
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
};
@@ -18,6 +19,7 @@ use object::{
RelocationEncoding, RelocationKind, SectionKind, SymbolFlags, SymbolKind, SymbolScope,
};
use std::collections::HashMap;
use std::convert::TryInto;
use std::mem;
use target_lexicon::PointerWidth;
@@ -208,14 +210,23 @@ impl Backend for ObjectBackend {
fn define_function<TS>(
&mut self,
func_id: FuncId,
ctx: &cranelift_codegen::Context,
ctx: &mut cranelift_codegen::Context,
declarations: &ModuleDeclarations,
code_size: u32,
trap_sink: &mut TS,
) -> ModuleResult<()>
) -> ModuleResult<ModuleCompiledFunction>
where
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);
if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
@@ -267,7 +278,8 @@ impl Backend for ObjectBackend {
relocs: reloc_sink.relocs,
});
}
Ok(())
Ok(ModuleCompiledFunction { size: code_size })
}
fn define_function_bytes(
@@ -275,12 +287,19 @@ impl Backend for ObjectBackend {
func_id: FuncId,
bytes: &[u8],
declarations: &ModuleDeclarations,
) -> ModuleResult<()> {
) -> ModuleResult<ModuleCompiledFunction> {
info!("defining function {} with bytes", func_id);
let decl = declarations.get_function_decl(func_id);
if !decl.linkage.is_definable() {
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();
if *defined {
return Err(ModuleError::DuplicateDefinition(decl.name.clone()));
@@ -304,7 +323,7 @@ impl Backend for ObjectBackend {
.add_symbol_data(symbol, section, bytes, self.function_alignment);
}
Ok(())
Ok(ModuleCompiledFunction { size: total_size })
}
fn define_data(

View File

@@ -19,6 +19,7 @@ libc = { version = "0.2.42" }
errno = "0.2.4"
target-lexicon = "0.11"
memmap = { version = "0.7.0", optional = true }
log = { version = "0.4.6", default-features = false }
[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["winbase", "memoryapi"] }

View File

@@ -2,7 +2,7 @@
use crate::memory::Memory;
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::settings::Configurable;
@@ -10,12 +10,14 @@ use cranelift_codegen::{self, ir, settings};
use cranelift_entity::SecondaryMap;
use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, FuncOrDataId, Init, Linkage,
ModuleDeclarations, ModuleError, ModuleResult,
ModuleCompiledFunction, ModuleDeclarations, ModuleError, ModuleResult,
};
use cranelift_native;
#[cfg(not(windows))]
use libc;
use log::info;
use std::collections::HashMap;
use std::convert::TryInto;
use std::ffi::CString;
use std::io::Write;
use std::ptr;
@@ -413,14 +415,19 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
fn define_function<TS>(
&mut self,
id: FuncId,
ctx: &cranelift_codegen::Context,
ctx: &mut cranelift_codegen::Context,
declarations: &ModuleDeclarations,
code_size: u32,
trap_sink: &mut TS,
) -> ModuleResult<()>
) -> ModuleResult<ModuleCompiledFunction>
where
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);
if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
@@ -458,7 +465,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
relocs: reloc_sink.relocs,
});
Ok(())
Ok(ModuleCompiledFunction { size: code_size })
}
fn define_function_bytes(
@@ -466,12 +473,17 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
id: FuncId,
bytes: &[u8],
declarations: &ModuleDeclarations,
) -> ModuleResult<()> {
) -> ModuleResult<ModuleCompiledFunction> {
let decl = declarations.get_function_decl(id);
if !decl.linkage.is_definable() {
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() {
return Err(ModuleError::DuplicateDefinition(decl.name.to_owned()));
}
@@ -496,7 +508,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
relocs: vec![],
});
Ok(())
Ok(ModuleCompiledFunction { size: total_size })
}
fn define_data(