Add cranelift-object

This commit is contained in:
Philip Craig
2019-05-06 16:34:16 +10:00
committed by Dan Gohman
parent 90b0b86f5c
commit 3293ca6b69
12 changed files with 977 additions and 22 deletions

View File

@@ -1,6 +1,8 @@
//! Defines the `Backend` trait.
use crate::DataContext;
use crate::DataId;
use crate::FuncId;
use crate::Linkage;
use crate::ModuleNamespace;
use crate::ModuleResult;
@@ -56,16 +58,24 @@ where
fn isa(&self) -> &dyn TargetIsa;
/// Declare a function.
fn declare_function(&mut self, name: &str, linkage: Linkage);
fn declare_function(&mut self, id: FuncId, name: &str, linkage: Linkage);
/// Declare a data object.
fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool, align: Option<u8>);
fn declare_data(
&mut self,
id: DataId,
name: &str,
linkage: Linkage,
writable: bool,
align: Option<u8>,
);
/// Define a function, producing the function body from the given `Context`.
///
/// Functions must be declared before being defined.
fn define_function(
&mut self,
id: FuncId,
name: &str,
ctx: &Context,
namespace: &ModuleNamespace<Self>,
@@ -77,6 +87,7 @@ where
/// Data objects must be declared before being defined.
fn define_data(
&mut self,
id: DataId,
name: &str,
writable: bool,
align: Option<u8>,
@@ -107,6 +118,7 @@ where
/// and `Export` entities referenced to be defined.
fn finalize_function(
&mut self,
id: FuncId,
func: &Self::CompiledFunction,
namespace: &ModuleNamespace<Self>,
) -> Self::FinalizedFunction;
@@ -118,6 +130,7 @@ where
/// `Local` and `Export` entities referenced to be defined.
fn finalize_data(
&mut self,
id: DataId,
data: &Self::CompiledData,
namespace: &ModuleNamespace<Self>,
) -> Self::FinalizedData;

View File

@@ -224,26 +224,32 @@ impl<B> ModuleContents<B>
where
B: Backend,
{
fn get_function_info(&self, name: &ir::ExternalName) -> &ModuleFunction<B> {
fn get_function_id(&self, name: &ir::ExternalName) -> FuncId {
if let ir::ExternalName::User { namespace, index } = *name {
debug_assert_eq!(namespace, 0);
let func = FuncId::from_u32(index);
&self.functions[func]
FuncId::from_u32(index)
} else {
panic!("unexpected ExternalName kind {}", name)
}
}
/// Get the `DataDeclaration` for the function named by `name`.
fn get_data_info(&self, name: &ir::ExternalName) -> &ModuleData<B> {
fn get_data_id(&self, name: &ir::ExternalName) -> DataId {
if let ir::ExternalName::User { namespace, index } = *name {
debug_assert_eq!(namespace, 1);
let data = DataId::from_u32(index);
&self.data_objects[data]
DataId::from_u32(index)
} else {
panic!("unexpected ExternalName kind {}", name)
}
}
fn get_function_info(&self, name: &ir::ExternalName) -> &ModuleFunction<B> {
&self.functions[self.get_function_id(name)]
}
/// Get the `DataDeclaration` for the function named by `name`.
fn get_data_info(&self, name: &ir::ExternalName) -> &ModuleData<B> {
&self.data_objects[self.get_data_id(name)]
}
}
/// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated
@@ -259,12 +265,22 @@ impl<'a, B> ModuleNamespace<'a, B>
where
B: Backend,
{
/// Get the `FuncId` for the function named by `name`.
pub fn get_function_id(&self, name: &ir::ExternalName) -> FuncId {
self.contents.get_function_id(name)
}
/// Get the `DataId` for the data object named by `name`.
pub fn get_data_id(&self, name: &ir::ExternalName) -> DataId {
self.contents.get_data_id(name)
}
/// Get the `FunctionDeclaration` for the function named by `name`.
pub fn get_function_decl(&self, name: &ir::ExternalName) -> &FunctionDeclaration {
&self.contents.get_function_info(name).decl
}
/// Get the `DataDeclaration` for the function named by `name`.
/// Get the `DataDeclaration` for the data object named by `name`.
pub fn get_data_decl(&self, name: &ir::ExternalName) -> &DataDeclaration {
&self.contents.get_data_info(name).decl
}
@@ -407,7 +423,8 @@ where
FuncOrDataId::Func(id) => {
let existing = &mut self.contents.functions[id];
existing.merge(linkage, signature)?;
self.backend.declare_function(name, existing.decl.linkage);
self.backend
.declare_function(id, name, existing.decl.linkage);
Ok(id)
}
FuncOrDataId::Data(..) => {
@@ -424,7 +441,7 @@ where
compiled: None,
});
entry.insert(FuncOrDataId::Func(id));
self.backend.declare_function(name, linkage);
self.backend.declare_function(id, name, linkage);
Ok(id)
}
}
@@ -451,6 +468,7 @@ where
let existing = &mut self.contents.data_objects[id];
existing.merge(linkage, writable, align);
self.backend.declare_data(
id,
name,
existing.decl.linkage,
existing.decl.writable,
@@ -474,7 +492,8 @@ where
compiled: None,
});
entry.insert(FuncOrDataId::Data(id));
self.backend.declare_data(name, linkage, writable, align);
self.backend
.declare_data(id, name, linkage, writable, align);
Ok(id)
}
}
@@ -536,7 +555,6 @@ where
);
ModuleError::Compilation(e)
})?;
let info = &self.contents.functions[func];
if info.compiled.is_some() {
return Err(ModuleError::DuplicateDefinition(info.decl.name.clone()));
@@ -546,6 +564,7 @@ where
}
let compiled = Some(self.backend.define_function(
func,
&info.decl.name,
ctx,
&ModuleNamespace::<B> {
@@ -570,6 +589,7 @@ where
return Err(ModuleError::InvalidImportDefinition(info.decl.name.clone()));
}
Some(self.backend.define_data(
data,
&info.decl.name,
info.decl.writable,
info.decl.align,
@@ -638,6 +658,7 @@ where
let info = &self.contents.functions[func];
debug_assert!(info.decl.linkage.is_definable());
self.backend.finalize_function(
func,
info.compiled
.as_ref()
.expect("function must be compiled before it can be finalized"),
@@ -650,6 +671,7 @@ where
let info = &self.contents.data_objects[data];
debug_assert!(info.decl.linkage.is_definable());
self.backend.finalize_data(
data,
info.compiled
.as_ref()
.expect("data object must be compiled before it can be finalized"),