Remove Backend trait and turn Module into a trait
This commit is contained in:
@@ -1,128 +0,0 @@
|
||||
//! 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 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;
|
||||
|
||||
/// A `Backend` implements the functionality needed to support a `Module`.
|
||||
///
|
||||
/// Three notable implementations of this trait are:
|
||||
/// - `SimpleJITBackend`, defined in [cranelift-simplejit], which JITs
|
||||
/// the contents of a `Module` to memory which can be directly executed.
|
||||
/// - `ObjectBackend`, defined in [cranelift-object], which writes the
|
||||
/// contents of a `Module` out as a native object file.
|
||||
///
|
||||
/// [cranelift-simplejit]: https://docs.rs/cranelift-simplejit/
|
||||
/// [cranelift-object]: https://docs.rs/cranelift-object/
|
||||
pub trait Backend
|
||||
where
|
||||
Self: marker::Sized,
|
||||
{
|
||||
/// A builder for constructing `Backend` instances.
|
||||
type Builder;
|
||||
|
||||
/// This is an object returned by `Module`'s
|
||||
/// [`finish`](struct.Module.html#method.finish) function,
|
||||
/// if the `Backend` has a purpose for this.
|
||||
type Product;
|
||||
|
||||
/// Create a new `Backend` instance.
|
||||
fn new(_: Self::Builder) -> Self;
|
||||
|
||||
/// Return the `TargetIsa` to compile for.
|
||||
fn isa(&self) -> &dyn TargetIsa;
|
||||
|
||||
/// Get all declarations in this module.
|
||||
fn declarations(&self) -> &ModuleDeclarations;
|
||||
|
||||
/// Declare a function.
|
||||
fn declare_function(
|
||||
&mut self,
|
||||
name: &str,
|
||||
linkage: Linkage,
|
||||
signature: &ir::Signature,
|
||||
) -> ModuleResult<FuncId>;
|
||||
|
||||
/// Declare a data object.
|
||||
fn declare_data(
|
||||
&mut self,
|
||||
name: &str,
|
||||
linkage: Linkage,
|
||||
writable: bool,
|
||||
tls: bool,
|
||||
) -> ModuleResult<DataId>;
|
||||
|
||||
/// Define a function, producing the function body from the given `Context`.
|
||||
///
|
||||
/// Functions must be declared before being defined.
|
||||
fn define_function<TS>(
|
||||
&mut self,
|
||||
func: FuncId,
|
||||
ctx: &mut Context,
|
||||
trap_sink: &mut TS,
|
||||
) -> ModuleResult<ModuleCompiledFunction>
|
||||
where
|
||||
TS: binemit::TrapSink;
|
||||
|
||||
/// Define a function, taking the function body from the given `bytes`.
|
||||
///
|
||||
/// Functions must be declared before being defined.
|
||||
fn define_function_bytes(
|
||||
&mut self,
|
||||
id: FuncId,
|
||||
bytes: &[u8],
|
||||
) -> ModuleResult<ModuleCompiledFunction>;
|
||||
|
||||
/// Define a zero-initialized data object of the given size.
|
||||
///
|
||||
/// Data objects must be declared before being defined.
|
||||
fn define_data(
|
||||
&mut self,
|
||||
id: DataId,
|
||||
data_ctx: &DataContext,
|
||||
) -> ModuleResult<()>;
|
||||
|
||||
/// Consume this `Backend` and return a result. Some implementations may
|
||||
/// provide additional functionality through this result.
|
||||
fn finish(self) -> Self::Product;
|
||||
}
|
||||
|
||||
/// Default names for `ir::LibCall`s. A function by this name is imported into the object as
|
||||
/// part of the translation of a `ir::ExternalName::LibCall` variant.
|
||||
pub fn default_libcall_names() -> Box<dyn Fn(ir::LibCall) -> String> {
|
||||
Box::new(move |libcall| match libcall {
|
||||
ir::LibCall::Probestack => "__cranelift_probestack".to_owned(),
|
||||
ir::LibCall::UdivI64 => "__udivdi3".to_owned(),
|
||||
ir::LibCall::SdivI64 => "__divdi3".to_owned(),
|
||||
ir::LibCall::UremI64 => "__umoddi3".to_owned(),
|
||||
ir::LibCall::SremI64 => "__moddi3".to_owned(),
|
||||
ir::LibCall::IshlI64 => "__ashldi3".to_owned(),
|
||||
ir::LibCall::UshrI64 => "__lshrdi3".to_owned(),
|
||||
ir::LibCall::SshrI64 => "__ashrdi3".to_owned(),
|
||||
ir::LibCall::CeilF32 => "ceilf".to_owned(),
|
||||
ir::LibCall::CeilF64 => "ceil".to_owned(),
|
||||
ir::LibCall::FloorF32 => "floorf".to_owned(),
|
||||
ir::LibCall::FloorF64 => "floor".to_owned(),
|
||||
ir::LibCall::TruncF32 => "truncf".to_owned(),
|
||||
ir::LibCall::TruncF64 => "trunc".to_owned(),
|
||||
ir::LibCall::NearestF32 => "nearbyintf".to_owned(),
|
||||
ir::LibCall::NearestF64 => "nearbyint".to_owned(),
|
||||
ir::LibCall::Memcpy => "memcpy".to_owned(),
|
||||
ir::LibCall::Memset => "memset".to_owned(),
|
||||
ir::LibCall::Memmove => "memmove".to_owned(),
|
||||
|
||||
ir::LibCall::ElfTlsGetAddr => "__tls_get_addr".to_owned(),
|
||||
})
|
||||
}
|
||||
@@ -29,15 +29,18 @@ extern crate std;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use hashbrown::{hash_map, HashMap};
|
||||
use std::borrow::ToOwned;
|
||||
use std::boxed::Box;
|
||||
#[cfg(feature = "std")]
|
||||
use std::collections::{hash_map, HashMap};
|
||||
use std::string::String;
|
||||
|
||||
use cranelift_codegen::ir;
|
||||
|
||||
mod backend;
|
||||
mod data_context;
|
||||
mod module;
|
||||
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, ModuleCompiledFunction, ModuleDeclarations,
|
||||
@@ -47,3 +50,31 @@ pub use crate::traps::TrapSite;
|
||||
|
||||
/// Version number of this crate.
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
/// Default names for `ir::LibCall`s. A function by this name is imported into the object as
|
||||
/// part of the translation of a `ir::ExternalName::LibCall` variant.
|
||||
pub fn default_libcall_names() -> Box<dyn Fn(ir::LibCall) -> String> {
|
||||
Box::new(move |libcall| match libcall {
|
||||
ir::LibCall::Probestack => "__cranelift_probestack".to_owned(),
|
||||
ir::LibCall::UdivI64 => "__udivdi3".to_owned(),
|
||||
ir::LibCall::SdivI64 => "__divdi3".to_owned(),
|
||||
ir::LibCall::UremI64 => "__umoddi3".to_owned(),
|
||||
ir::LibCall::SremI64 => "__moddi3".to_owned(),
|
||||
ir::LibCall::IshlI64 => "__ashldi3".to_owned(),
|
||||
ir::LibCall::UshrI64 => "__lshrdi3".to_owned(),
|
||||
ir::LibCall::SshrI64 => "__ashrdi3".to_owned(),
|
||||
ir::LibCall::CeilF32 => "ceilf".to_owned(),
|
||||
ir::LibCall::CeilF64 => "ceil".to_owned(),
|
||||
ir::LibCall::FloorF32 => "floorf".to_owned(),
|
||||
ir::LibCall::FloorF64 => "floor".to_owned(),
|
||||
ir::LibCall::TruncF32 => "truncf".to_owned(),
|
||||
ir::LibCall::TruncF64 => "trunc".to_owned(),
|
||||
ir::LibCall::NearestF32 => "nearbyintf".to_owned(),
|
||||
ir::LibCall::NearestF64 => "nearbyint".to_owned(),
|
||||
ir::LibCall::Memcpy => "memcpy".to_owned(),
|
||||
ir::LibCall::Memset => "memset".to_owned(),
|
||||
ir::LibCall::Memmove => "memmove".to_owned(),
|
||||
|
||||
ir::LibCall::ElfTlsGetAddr => "__tls_get_addr".to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
use super::HashMap;
|
||||
use crate::data_context::DataContext;
|
||||
use crate::Backend;
|
||||
use cranelift_codegen::binemit;
|
||||
use cranelift_codegen::entity::{entity_impl, PrimaryMap};
|
||||
use cranelift_codegen::{ir, isa, CodegenError, Context};
|
||||
@@ -318,40 +317,29 @@ impl ModuleDeclarations {
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Module` is a utility for collecting functions and data objects, and linking them together.
|
||||
pub struct Module<B>
|
||||
where
|
||||
B: Backend,
|
||||
{
|
||||
backend: B,
|
||||
}
|
||||
|
||||
/// Information about the compiled function.
|
||||
pub struct ModuleCompiledFunction {
|
||||
/// The size of the compiled function.
|
||||
pub size: binemit::CodeOffset,
|
||||
}
|
||||
|
||||
impl<B> Module<B>
|
||||
where
|
||||
B: Backend,
|
||||
{
|
||||
/// Create a new `Module`.
|
||||
pub fn new(backend_builder: B::Builder) -> Self {
|
||||
Self {
|
||||
backend: B::new(backend_builder),
|
||||
}
|
||||
}
|
||||
/// A `Module` is a utility for collecting functions and data objects, and linking them together.
|
||||
pub trait Module {
|
||||
/// Return the `TargetIsa` to compile for.
|
||||
fn isa(&self) -> &dyn isa::TargetIsa;
|
||||
|
||||
/// Get all declarations in this module.
|
||||
fn declarations(&self) -> &ModuleDeclarations;
|
||||
|
||||
/// Get the module identifier for a given name, if that name
|
||||
/// has been declared.
|
||||
pub fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
|
||||
self.backend.declarations().get_name(name)
|
||||
fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
|
||||
self.declarations().get_name(name)
|
||||
}
|
||||
|
||||
/// Return the target information needed by frontends to produce Cranelift IR
|
||||
/// for the current target.
|
||||
pub fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
self.isa().frontend_config()
|
||||
}
|
||||
|
||||
@@ -359,7 +347,7 @@ where
|
||||
///
|
||||
/// This ensures that the `Context` is initialized with the default calling
|
||||
/// convention for the `TargetIsa`.
|
||||
pub fn make_context(&self) -> Context {
|
||||
fn make_context(&self) -> Context {
|
||||
let mut ctx = Context::new();
|
||||
ctx.func.signature.call_conv = self.isa().default_call_conv();
|
||||
ctx
|
||||
@@ -369,7 +357,7 @@ where
|
||||
///
|
||||
/// This ensures that the `Context` is initialized with the default calling
|
||||
/// convention for the `TargetIsa`.
|
||||
pub fn clear_context(&self, ctx: &mut Context) {
|
||||
fn clear_context(&self, ctx: &mut Context) {
|
||||
ctx.clear();
|
||||
ctx.func.signature.call_conv = self.isa().default_call_conv();
|
||||
}
|
||||
@@ -377,7 +365,7 @@ where
|
||||
/// Create a new empty `Signature` with the default calling convention for
|
||||
/// the `TargetIsa`, to which parameter and return types can be added for
|
||||
/// declaring a function to be called by this `Module`.
|
||||
pub fn make_signature(&self) -> ir::Signature {
|
||||
fn make_signature(&self) -> ir::Signature {
|
||||
ir::Signature::new(self.isa().default_call_conv())
|
||||
}
|
||||
|
||||
@@ -385,38 +373,33 @@ where
|
||||
///
|
||||
/// This ensures that the `Signature` is initialized with the default
|
||||
/// calling convention for the `TargetIsa`.
|
||||
pub fn clear_signature(&self, sig: &mut ir::Signature) {
|
||||
fn clear_signature(&self, sig: &mut ir::Signature) {
|
||||
sig.clear(self.isa().default_call_conv());
|
||||
}
|
||||
|
||||
/// Declare a function in this module.
|
||||
pub fn declare_function(
|
||||
fn declare_function(
|
||||
&mut self,
|
||||
name: &str,
|
||||
linkage: Linkage,
|
||||
signature: &ir::Signature,
|
||||
) -> ModuleResult<FuncId> {
|
||||
self.backend.declare_function(name, linkage, signature)
|
||||
}
|
||||
) -> ModuleResult<FuncId>;
|
||||
|
||||
/// Declare a data object in this module.
|
||||
pub fn declare_data(
|
||||
fn declare_data(
|
||||
&mut self,
|
||||
name: &str,
|
||||
linkage: Linkage,
|
||||
writable: bool,
|
||||
tls: bool,
|
||||
) -> ModuleResult<DataId> {
|
||||
self.backend
|
||||
.declare_data(name, linkage, writable, tls)
|
||||
}
|
||||
) -> ModuleResult<DataId>;
|
||||
|
||||
/// Use this when you're building the IR of a function to reference a function.
|
||||
///
|
||||
/// TODO: Coalesce redundant decls and signatures.
|
||||
/// 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 {
|
||||
let decl = &self.backend.declarations().functions[func];
|
||||
fn declare_func_in_func(&self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
|
||||
let decl = &self.declarations().functions[func];
|
||||
let signature = in_func.import_signature(decl.signature.clone());
|
||||
let colocated = decl.linkage.is_final();
|
||||
in_func.import_function(ir::ExtFuncData {
|
||||
@@ -429,8 +412,8 @@ where
|
||||
/// Use this when you're building the IR of a function to reference a data object.
|
||||
///
|
||||
/// TODO: Same as above.
|
||||
pub fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
|
||||
let decl = &self.backend.declarations().data_objects[data];
|
||||
fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
|
||||
let decl = &self.declarations().data_objects[data];
|
||||
let colocated = decl.linkage.is_final();
|
||||
func.create_global_value(ir::GlobalValueData::Symbol {
|
||||
name: ir::ExternalName::user(1, data.as_u32()),
|
||||
@@ -441,12 +424,12 @@ where
|
||||
}
|
||||
|
||||
/// TODO: Same as above.
|
||||
pub fn declare_func_in_data(&self, func: FuncId, ctx: &mut DataContext) -> ir::FuncRef {
|
||||
fn declare_func_in_data(&self, func: FuncId, ctx: &mut DataContext) -> ir::FuncRef {
|
||||
ctx.import_function(ir::ExternalName::user(0, func.as_u32()))
|
||||
}
|
||||
|
||||
/// TODO: Same as above.
|
||||
pub fn declare_data_in_data(&self, data: DataId, ctx: &mut DataContext) -> ir::GlobalValue {
|
||||
fn declare_data_in_data(&self, data: DataId, ctx: &mut DataContext) -> ir::GlobalValue {
|
||||
ctx.import_global_value(ir::ExternalName::user(1, data.as_u32()))
|
||||
}
|
||||
|
||||
@@ -455,17 +438,14 @@ where
|
||||
/// Returns the size of the function's code and constant data.
|
||||
///
|
||||
/// Note: After calling this function the given `Context` will contain the compiled function.
|
||||
pub fn define_function<TS>(
|
||||
fn define_function<TS>(
|
||||
&mut self,
|
||||
func: FuncId,
|
||||
ctx: &mut Context,
|
||||
trap_sink: &mut TS,
|
||||
) -> ModuleResult<ModuleCompiledFunction>
|
||||
where
|
||||
TS: binemit::TrapSink,
|
||||
{
|
||||
self.backend.define_function(func, ctx, trap_sink)
|
||||
}
|
||||
TS: binemit::TrapSink;
|
||||
|
||||
/// Define a function, taking the function body from the given `bytes`.
|
||||
///
|
||||
@@ -474,28 +454,12 @@ where
|
||||
/// `define_function`.
|
||||
///
|
||||
/// Returns the size of the function's code.
|
||||
pub fn define_function_bytes(
|
||||
fn define_function_bytes(
|
||||
&mut self,
|
||||
func: FuncId,
|
||||
bytes: &[u8],
|
||||
) -> ModuleResult<ModuleCompiledFunction> {
|
||||
self.backend.define_function_bytes(func, bytes)
|
||||
}
|
||||
) -> ModuleResult<ModuleCompiledFunction>;
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// Return the target isa
|
||||
pub fn isa(&self) -> &dyn isa::TargetIsa {
|
||||
self.backend.isa()
|
||||
}
|
||||
|
||||
/// Consume the module and return the resulting `Product`. Some `Backend`
|
||||
/// implementations may provide additional functionality available after
|
||||
/// a `Module` is complete.
|
||||
pub fn finish(self) -> B::Product {
|
||||
self.backend.finish()
|
||||
}
|
||||
fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user