use crate::DataContext;
use crate::DataId;
use crate::FuncId;
use crate::Linkage;
use crate::ModuleNamespace;
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;
pub trait Backend
where
Self: marker::Sized,
{
type Builder;
type CompiledFunction;
type CompiledData;
type FinalizedFunction;
type FinalizedData;
type Product;
fn new(_: Self::Builder) -> Self;
fn isa(&self) -> &dyn TargetIsa;
fn declare_function(&mut self, id: FuncId, name: &str, linkage: Linkage);
fn declare_data(
&mut self,
id: DataId,
name: &str,
linkage: Linkage,
writable: bool,
tls: bool,
align: Option<u8>,
);
fn define_function<TS>(
&mut self,
id: FuncId,
name: &str,
ctx: &Context,
namespace: &ModuleNamespace<Self>,
code_size: u32,
trap_sink: &mut TS,
) -> ModuleResult<Self::CompiledFunction>
where
TS: binemit::TrapSink;
fn define_function_bytes(
&mut self,
id: FuncId,
name: &str,
bytes: &[u8],
namespace: &ModuleNamespace<Self>,
) -> ModuleResult<Self::CompiledFunction>;
fn define_data(
&mut self,
id: DataId,
name: &str,
writable: bool,
tls: bool,
align: Option<u8>,
data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>,
) -> ModuleResult<Self::CompiledData>;
fn write_data_funcaddr(
&mut self,
data: &mut Self::CompiledData,
offset: usize,
what: ir::FuncRef,
);
fn write_data_dataaddr(
&mut self,
data: &mut Self::CompiledData,
offset: usize,
what: ir::GlobalValue,
addend: binemit::Addend,
);
fn finalize_function(
&mut self,
id: FuncId,
func: &Self::CompiledFunction,
namespace: &ModuleNamespace<Self>,
) -> Self::FinalizedFunction;
fn get_finalized_function(&self, func: &Self::CompiledFunction) -> Self::FinalizedFunction;
fn finalize_data(
&mut self,
id: DataId,
data: &Self::CompiledData,
namespace: &ModuleNamespace<Self>,
) -> Self::FinalizedData;
fn get_finalized_data(&self, data: &Self::CompiledData) -> Self::FinalizedData;
fn publish(&mut self);
fn finish(self, namespace: &ModuleNamespace<Self>) -> Self::Product;
}
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::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(),
})
}