Introduce a ModuleResult alias for Result<T, ModuleError>.
This follows the pattern used by cretonne-codegen, cretonne-wasm, and others.
This commit is contained in:
@@ -5,7 +5,7 @@ use cretonne_codegen::binemit::{Addend, CodeOffset, NullTrapSink, Reloc, RelocSi
|
|||||||
use cretonne_codegen::isa::TargetIsa;
|
use cretonne_codegen::isa::TargetIsa;
|
||||||
use cretonne_codegen::{self, binemit, ir};
|
use cretonne_codegen::{self, binemit, ir};
|
||||||
use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, ModuleError,
|
use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, ModuleError,
|
||||||
ModuleNamespace};
|
ModuleNamespace, ModuleResult};
|
||||||
use faerie;
|
use faerie;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@@ -52,7 +52,7 @@ impl FaerieBuilder {
|
|||||||
format: BinaryFormat,
|
format: BinaryFormat,
|
||||||
collect_traps: FaerieTrapCollection,
|
collect_traps: FaerieTrapCollection,
|
||||||
libcall_names: Box<Fn(ir::LibCall) -> String>,
|
libcall_names: Box<Fn(ir::LibCall) -> String>,
|
||||||
) -> Result<Self, ModuleError> {
|
) -> ModuleResult<Self> {
|
||||||
if !isa.flags().is_pic() {
|
if !isa.flags().is_pic() {
|
||||||
return Err(ModuleError::Backend(
|
return Err(ModuleError::Backend(
|
||||||
"faerie requires TargetIsa be PIC".to_owned(),
|
"faerie requires TargetIsa be PIC".to_owned(),
|
||||||
@@ -149,7 +149,7 @@ impl Backend for FaerieBackend {
|
|||||||
ctx: &cretonne_codegen::Context,
|
ctx: &cretonne_codegen::Context,
|
||||||
namespace: &ModuleNamespace<Self>,
|
namespace: &ModuleNamespace<Self>,
|
||||||
code_size: u32,
|
code_size: u32,
|
||||||
) -> Result<FaerieCompiledFunction, ModuleError> {
|
) -> ModuleResult<FaerieCompiledFunction> {
|
||||||
let mut code: Vec<u8> = Vec::with_capacity(code_size as usize);
|
let mut code: Vec<u8> = Vec::with_capacity(code_size as usize);
|
||||||
code.resize(code_size as usize, 0);
|
code.resize(code_size as usize, 0);
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ impl Backend for FaerieBackend {
|
|||||||
name: &str,
|
name: &str,
|
||||||
data_ctx: &DataContext,
|
data_ctx: &DataContext,
|
||||||
namespace: &ModuleNamespace<Self>,
|
namespace: &ModuleNamespace<Self>,
|
||||||
) -> Result<FaerieCompiledData, ModuleError> {
|
) -> ModuleResult<FaerieCompiledData> {
|
||||||
let &DataDescription {
|
let &DataDescription {
|
||||||
writable: _writable,
|
writable: _writable,
|
||||||
ref init,
|
ref init,
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use DataContext;
|
|||||||
use Linkage;
|
use Linkage;
|
||||||
use ModuleError;
|
use ModuleError;
|
||||||
use ModuleNamespace;
|
use ModuleNamespace;
|
||||||
|
use ModuleResult;
|
||||||
|
|
||||||
/// A `Backend` implements the functionality needed to support a `Module`.
|
/// A `Backend` implements the functionality needed to support a `Module`.
|
||||||
pub trait Backend
|
pub trait Backend
|
||||||
@@ -57,7 +58,7 @@ where
|
|||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
namespace: &ModuleNamespace<Self>,
|
namespace: &ModuleNamespace<Self>,
|
||||||
code_size: u32,
|
code_size: u32,
|
||||||
) -> Result<Self::CompiledFunction, ModuleError>;
|
) -> ModuleResult<Self::CompiledFunction>;
|
||||||
|
|
||||||
/// Define a zero-initialized data object of the given size.
|
/// Define a zero-initialized data object of the given size.
|
||||||
///
|
///
|
||||||
@@ -67,7 +68,7 @@ where
|
|||||||
name: &str,
|
name: &str,
|
||||||
data_ctx: &DataContext,
|
data_ctx: &DataContext,
|
||||||
namespace: &ModuleNamespace<Self>,
|
namespace: &ModuleNamespace<Self>,
|
||||||
) -> Result<Self::CompiledData, ModuleError>;
|
) -> ModuleResult<Self::CompiledData>;
|
||||||
|
|
||||||
/// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a
|
/// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a
|
||||||
/// defined data object.
|
/// defined data object.
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ mod module;
|
|||||||
|
|
||||||
pub use backend::Backend;
|
pub use backend::Backend;
|
||||||
pub use data_context::{DataContext, DataDescription, Init, Writability};
|
pub use data_context::{DataContext, DataDescription, Init, Writability};
|
||||||
pub use module::{DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleNamespace};
|
pub use module::{DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleNamespace,
|
||||||
|
ModuleResult};
|
||||||
|
|
||||||
/// This replaces `std` in builds with `core`.
|
/// This replaces `std` in builds with `core`.
|
||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
|
|||||||
@@ -139,6 +139,9 @@ pub enum ModuleError {
|
|||||||
Backend(String),
|
Backend(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A convenient alias for a `Result` that uses `ModuleError` as the error type.
|
||||||
|
pub type ModuleResult<T> = Result<T, ModuleError>;
|
||||||
|
|
||||||
/// A function belonging to a `Module`.
|
/// A function belonging to a `Module`.
|
||||||
struct ModuleFunction<B>
|
struct ModuleFunction<B>
|
||||||
where
|
where
|
||||||
@@ -347,7 +350,7 @@ where
|
|||||||
name: &str,
|
name: &str,
|
||||||
linkage: Linkage,
|
linkage: Linkage,
|
||||||
signature: &ir::Signature,
|
signature: &ir::Signature,
|
||||||
) -> Result<FuncId, ModuleError> {
|
) -> ModuleResult<FuncId> {
|
||||||
// TODO: Can we avoid allocating names so often?
|
// TODO: Can we avoid allocating names so often?
|
||||||
use std::collections::hash_map::Entry::*;
|
use std::collections::hash_map::Entry::*;
|
||||||
match self.names.entry(name.to_owned()) {
|
match self.names.entry(name.to_owned()) {
|
||||||
@@ -385,7 +388,7 @@ where
|
|||||||
name: &str,
|
name: &str,
|
||||||
linkage: Linkage,
|
linkage: Linkage,
|
||||||
writable: bool,
|
writable: bool,
|
||||||
) -> Result<DataId, ModuleError> {
|
) -> ModuleResult<DataId> {
|
||||||
// TODO: Can we avoid allocating names so often?
|
// TODO: Can we avoid allocating names so often?
|
||||||
use std::collections::hash_map::Entry::*;
|
use std::collections::hash_map::Entry::*;
|
||||||
match self.names.entry(name.to_owned()) {
|
match self.names.entry(name.to_owned()) {
|
||||||
@@ -457,7 +460,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Define a function, producing the function body from the given `Context`.
|
/// Define a function, producing the function body from the given `Context`.
|
||||||
pub fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> Result<(), ModuleError> {
|
pub fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> ModuleResult<()> {
|
||||||
let compiled = {
|
let compiled = {
|
||||||
let code_size = ctx.compile(self.backend.isa()).map_err(|e| {
|
let code_size = ctx.compile(self.backend.isa()).map_err(|e| {
|
||||||
dbg!(
|
dbg!(
|
||||||
@@ -489,7 +492,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Define a function, producing the data contents from the given `DataContext`.
|
/// Define a function, producing the data contents from the given `DataContext`.
|
||||||
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> Result<(), ModuleError> {
|
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
|
||||||
let compiled = {
|
let compiled = {
|
||||||
let info = &self.contents.data_objects[data];
|
let info = &self.contents.data_objects[data];
|
||||||
if !info.compiled.is_none() {
|
if !info.compiled.is_none() {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
use cretonne_codegen::binemit::{Addend, CodeOffset, NullTrapSink, Reloc, RelocSink};
|
use cretonne_codegen::binemit::{Addend, CodeOffset, NullTrapSink, Reloc, RelocSink};
|
||||||
use cretonne_codegen::isa::TargetIsa;
|
use cretonne_codegen::isa::TargetIsa;
|
||||||
use cretonne_codegen::{self, ir, settings};
|
use cretonne_codegen::{self, ir, settings};
|
||||||
use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, ModuleError,
|
use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, ModuleNamespace,
|
||||||
ModuleNamespace, Writability};
|
ModuleResult, Writability};
|
||||||
use cretonne_native;
|
use cretonne_native;
|
||||||
use libc;
|
use libc;
|
||||||
use memory::Memory;
|
use memory::Memory;
|
||||||
@@ -118,7 +118,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
|||||||
ctx: &cretonne_codegen::Context,
|
ctx: &cretonne_codegen::Context,
|
||||||
_namespace: &ModuleNamespace<Self>,
|
_namespace: &ModuleNamespace<Self>,
|
||||||
code_size: u32,
|
code_size: u32,
|
||||||
) -> Result<Self::CompiledFunction, ModuleError> {
|
) -> ModuleResult<Self::CompiledFunction> {
|
||||||
let size = code_size as usize;
|
let size = code_size as usize;
|
||||||
let ptr = self.code_memory
|
let ptr = self.code_memory
|
||||||
.allocate(size)
|
.allocate(size)
|
||||||
@@ -141,7 +141,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
|||||||
_name: &str,
|
_name: &str,
|
||||||
data: &DataContext,
|
data: &DataContext,
|
||||||
_namespace: &ModuleNamespace<Self>,
|
_namespace: &ModuleNamespace<Self>,
|
||||||
) -> Result<Self::CompiledData, ModuleError> {
|
) -> ModuleResult<Self::CompiledData> {
|
||||||
let &DataDescription {
|
let &DataDescription {
|
||||||
writable,
|
writable,
|
||||||
ref init,
|
ref init,
|
||||||
|
|||||||
Reference in New Issue
Block a user