Make CtonError parameterized on the result type.

This makes it more consistent with other custom `Result` types.
This commit is contained in:
Dan Gohman
2018-06-07 15:06:31 -07:00
parent 13f22065a2
commit 02e34d1bf7
8 changed files with 32 additions and 32 deletions

View File

@@ -32,12 +32,12 @@ use cursor::{Cursor, FuncCursor};
use ir::{Function, InstructionData, Opcode}; use ir::{Function, InstructionData, Opcode};
use isa::{EncInfo, TargetIsa}; use isa::{EncInfo, TargetIsa};
use iterators::IteratorExtras; use iterators::IteratorExtras;
use result::CtonError; use result::CtonResult;
/// Relax branches and compute the final layout of EBB headers in `func`. /// Relax branches and compute the final layout of EBB headers in `func`.
/// ///
/// Fill in the `func.offsets` table so the function is ready for binary emission. /// Fill in the `func.offsets` table so the function is ready for binary emission.
pub fn relax_branches(func: &mut Function, isa: &TargetIsa) -> Result<CodeOffset, CtonError> { pub fn relax_branches(func: &mut Function, isa: &TargetIsa) -> CtonResult<CodeOffset> {
let encinfo = isa.encoding_info(); let encinfo = isa.encoding_info();
// Clear all offsets so we can recognize EBBs that haven't been visited yet. // Clear all offsets so we can recognize EBBs that haven't been visited yet.

View File

@@ -22,7 +22,7 @@ use nan_canonicalization::do_nan_canonicalization;
use postopt::do_postopt; use postopt::do_postopt;
use preopt::do_preopt; use preopt::do_preopt;
use regalloc; use regalloc;
use result::{CtonError, CtonResult}; use result::CtonResult;
use settings::{FlagsOrIsa, OptLevel}; use settings::{FlagsOrIsa, OptLevel};
use simple_gvn::do_simple_gvn; use simple_gvn::do_simple_gvn;
use std::vec::Vec; use std::vec::Vec;
@@ -95,7 +95,7 @@ impl Context {
mem: &mut Vec<u8>, mem: &mut Vec<u8>,
relocs: &mut RelocSink, relocs: &mut RelocSink,
traps: &mut TrapSink, traps: &mut TrapSink,
) -> CtonResult { ) -> CtonResult<()> {
let code_size = self.compile(isa)?; let code_size = self.compile(isa)?;
let old_len = mem.len(); let old_len = mem.len();
mem.resize(old_len + code_size as usize, 0); mem.resize(old_len + code_size as usize, 0);
@@ -117,7 +117,7 @@ impl Context {
/// code sink. /// code sink.
/// ///
/// Returns the size of the function's code. /// Returns the size of the function's code.
pub fn compile(&mut self, isa: &TargetIsa) -> Result<CodeOffset, CtonError> { pub fn compile(&mut self, isa: &TargetIsa) -> CtonResult<CodeOffset> {
let _tt = timing::compile(); let _tt = timing::compile();
self.verify_if(isa)?; self.verify_if(isa)?;
@@ -179,7 +179,7 @@ impl Context {
} }
/// Run the verifier only if the `enable_verifier` setting is true. /// Run the verifier only if the `enable_verifier` setting is true.
pub fn verify_if<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> CtonResult { pub fn verify_if<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> CtonResult<()> {
let fisa = fisa.into(); let fisa = fisa.into();
if fisa.flags.enable_verifier() { if fisa.flags.enable_verifier() {
self.verify(fisa).map_err(Into::into) self.verify(fisa).map_err(Into::into)
@@ -194,7 +194,7 @@ impl Context {
} }
/// Run the locations verifier only if the `enable_verifier` setting is true. /// Run the locations verifier only if the `enable_verifier` setting is true.
pub fn verify_locations_if(&self, isa: &TargetIsa) -> CtonResult { pub fn verify_locations_if(&self, isa: &TargetIsa) -> CtonResult<()> {
if isa.flags().enable_verifier() { if isa.flags().enable_verifier() {
self.verify_locations(isa).map_err(Into::into) self.verify_locations(isa).map_err(Into::into)
} else { } else {
@@ -203,27 +203,27 @@ impl Context {
} }
/// Perform dead-code elimination on the function. /// Perform dead-code elimination on the function.
pub fn dce<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CtonResult { pub fn dce<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CtonResult<()> {
do_dce(&mut self.func, &mut self.domtree); do_dce(&mut self.func, &mut self.domtree);
self.verify_if(fisa)?; self.verify_if(fisa)?;
Ok(()) Ok(())
} }
/// Perform pre-legalization rewrites on the function. /// Perform pre-legalization rewrites on the function.
pub fn preopt(&mut self, isa: &TargetIsa) -> CtonResult { pub fn preopt(&mut self, isa: &TargetIsa) -> CtonResult<()> {
do_preopt(&mut self.func); do_preopt(&mut self.func);
self.verify_if(isa)?; self.verify_if(isa)?;
Ok(()) Ok(())
} }
/// Perform NaN canonicalizing rewrites on the function. /// Perform NaN canonicalizing rewrites on the function.
pub fn canonicalize_nans(&mut self, isa: &TargetIsa) -> CtonResult { pub fn canonicalize_nans(&mut self, isa: &TargetIsa) -> CtonResult<()> {
do_nan_canonicalization(&mut self.func); do_nan_canonicalization(&mut self.func);
self.verify_if(isa) self.verify_if(isa)
} }
/// Run the legalizer for `isa` on the function. /// Run the legalizer for `isa` on the function.
pub fn legalize(&mut self, isa: &TargetIsa) -> CtonResult { pub fn legalize(&mut self, isa: &TargetIsa) -> CtonResult<()> {
// Legalization invalidates the domtree and loop_analysis by mutating the CFG. // Legalization invalidates the domtree and loop_analysis by mutating the CFG.
// TODO: Avoid doing this when legalization doesn't actually mutate the CFG. // TODO: Avoid doing this when legalization doesn't actually mutate the CFG.
self.domtree.clear(); self.domtree.clear();
@@ -233,7 +233,7 @@ impl Context {
} }
/// Perform post-legalization rewrites on the function. /// Perform post-legalization rewrites on the function.
pub fn postopt(&mut self, isa: &TargetIsa) -> CtonResult { pub fn postopt(&mut self, isa: &TargetIsa) -> CtonResult<()> {
do_postopt(&mut self.func, isa); do_postopt(&mut self.func, isa);
self.verify_if(isa)?; self.verify_if(isa)?;
Ok(()) Ok(())
@@ -262,13 +262,13 @@ impl Context {
} }
/// Perform simple GVN on the function. /// Perform simple GVN on the function.
pub fn simple_gvn<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CtonResult { pub fn simple_gvn<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CtonResult<()> {
do_simple_gvn(&mut self.func, &mut self.domtree); do_simple_gvn(&mut self.func, &mut self.domtree);
self.verify_if(fisa) self.verify_if(fisa)
} }
/// Perform LICM on the function. /// Perform LICM on the function.
pub fn licm<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CtonResult { pub fn licm<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CtonResult<()> {
do_licm( do_licm(
&mut self.func, &mut self.func,
&mut self.cfg, &mut self.cfg,
@@ -279,7 +279,7 @@ impl Context {
} }
/// Perform unreachable code elimination. /// Perform unreachable code elimination.
pub fn eliminate_unreachable_code<'a, FOI>(&mut self, fisa: FOI) -> CtonResult pub fn eliminate_unreachable_code<'a, FOI>(&mut self, fisa: FOI) -> CtonResult<()>
where where
FOI: Into<FlagsOrIsa<'a>>, FOI: Into<FlagsOrIsa<'a>>,
{ {
@@ -288,13 +288,13 @@ impl Context {
} }
/// Run the register allocator. /// Run the register allocator.
pub fn regalloc(&mut self, isa: &TargetIsa) -> CtonResult { pub fn regalloc(&mut self, isa: &TargetIsa) -> CtonResult<()> {
self.regalloc self.regalloc
.run(isa, &mut self.func, &self.cfg, &mut self.domtree) .run(isa, &mut self.func, &self.cfg, &mut self.domtree)
} }
/// Insert prologue and epilogues after computing the stack frame layout. /// Insert prologue and epilogues after computing the stack frame layout.
pub fn prologue_epilogue(&mut self, isa: &TargetIsa) -> CtonResult { pub fn prologue_epilogue(&mut self, isa: &TargetIsa) -> CtonResult<()> {
assert!( assert!(
self.func.stack_limit.is_none(), self.func.stack_limit.is_none(),
"stack_limit isn't implemented yet" "stack_limit isn't implemented yet"
@@ -306,7 +306,7 @@ impl Context {
} }
/// Run the instruction shrinking pass. /// Run the instruction shrinking pass.
pub fn shrink_instructions(&mut self, isa: &TargetIsa) -> CtonResult { pub fn shrink_instructions(&mut self, isa: &TargetIsa) -> CtonResult<()> {
shrink_instructions(&mut self.func, isa); shrink_instructions(&mut self.func, isa);
self.verify_if(isa)?; self.verify_if(isa)?;
self.verify_locations_if(isa)?; self.verify_locations_if(isa)?;
@@ -314,7 +314,7 @@ impl Context {
} }
/// Run the branch relaxation pass and return the final code size. /// Run the branch relaxation pass and return the final code size.
pub fn relax_branches(&mut self, isa: &TargetIsa) -> Result<CodeOffset, CtonError> { pub fn relax_branches(&mut self, isa: &TargetIsa) -> CtonResult<CodeOffset> {
let code_size = relax_branches(&mut self.func, isa)?; let code_size = relax_branches(&mut self.func, isa)?;
self.verify_if(isa)?; self.verify_if(isa)?;
self.verify_locations_if(isa)?; self.verify_locations_if(isa)?;

View File

@@ -56,7 +56,7 @@ use flowgraph;
use ir; use ir;
use isa::enc_tables::Encodings; use isa::enc_tables::Encodings;
use regalloc; use regalloc;
use result; use result::CtonResult;
use settings; use settings;
use settings::CallConv; use settings::CallConv;
use std::boxed::Box; use std::boxed::Box;
@@ -281,7 +281,7 @@ pub trait TargetIsa: fmt::Display {
/// Compute the stack layout and insert prologue and epilogue code into `func`. /// Compute the stack layout and insert prologue and epilogue code into `func`.
/// ///
/// Return an error if the stack frame is too large. /// Return an error if the stack frame is too large.
fn prologue_epilogue(&self, func: &mut ir::Function) -> result::CtonResult { fn prologue_epilogue(&self, func: &mut ir::Function) -> CtonResult<()> {
let _tt = timing::prologue_epilogue(); let _tt = timing::prologue_epilogue();
// This default implementation is unlikely to be good enough. // This default implementation is unlikely to be good enough.
use ir::stackslot::{StackOffset, StackSize}; use ir::stackslot::{StackOffset, StackSize};

View File

@@ -10,7 +10,7 @@ use ir::{get_probestack_funcref, AbiParam, ArgumentExtension, ArgumentLoc, Argum
InstBuilder, ValueLoc}; InstBuilder, ValueLoc};
use isa::{RegClass, RegUnit, TargetIsa}; use isa::{RegClass, RegUnit, TargetIsa};
use regalloc::RegisterSet; use regalloc::RegisterSet;
use result; use result::CtonResult;
use settings::CallConv; use settings::CallConv;
use stack_layout::layout_stack; use stack_layout::layout_stack;
use std::i32; use std::i32;
@@ -257,7 +257,7 @@ fn callee_saved_gprs_used(isa: &TargetIsa, func: &ir::Function) -> RegisterSet {
used used
} }
pub fn prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> result::CtonResult { pub fn prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> CtonResult<()> {
match func.signature.call_conv { match func.signature.call_conv {
// For now, just translate fast and cold as system_v. // For now, just translate fast and cold as system_v.
CallConv::Fast | CallConv::Cold | CallConv::SystemV => { CallConv::Fast | CallConv::Cold | CallConv::SystemV => {
@@ -269,7 +269,7 @@ pub fn prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> result::Ct
} }
} }
pub fn baldrdash_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> result::CtonResult { pub fn baldrdash_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> CtonResult<()> {
debug_assert!( debug_assert!(
!isa.flags().probestack_enabled(), !isa.flags().probestack_enabled(),
"baldrdash does not expect cretonne to emit stack probes" "baldrdash does not expect cretonne to emit stack probes"
@@ -290,7 +290,7 @@ pub fn baldrdash_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) ->
/// Implementation of the fastcall-based Win64 calling convention described at [1] /// Implementation of the fastcall-based Win64 calling convention described at [1]
/// [1] https://msdn.microsoft.com/en-us/library/ms235286.aspx /// [1] https://msdn.microsoft.com/en-us/library/ms235286.aspx
pub fn fastcall_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> result::CtonResult { pub fn fastcall_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> CtonResult<()> {
if isa.triple().pointer_width().unwrap() != PointerWidth::U64 { if isa.triple().pointer_width().unwrap() != PointerWidth::U64 {
panic!("TODO: windows-fastcall: x86-32 not implemented yet"); panic!("TODO: windows-fastcall: x86-32 not implemented yet");
} }
@@ -362,7 +362,7 @@ pub fn fastcall_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> r
} }
/// Insert a System V-compatible prologue and epilogue. /// Insert a System V-compatible prologue and epilogue.
pub fn system_v_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> result::CtonResult { pub fn system_v_prologue_epilogue(func: &mut ir::Function, isa: &TargetIsa) -> CtonResult<()> {
// The original 32-bit x86 ELF ABI had a 4-byte aligned stack pointer, but // The original 32-bit x86 ELF ABI had a 4-byte aligned stack pointer, but
// newer versions use a 16-byte aligned stack pointer. // newer versions use a 16-byte aligned stack pointer.
let stack_align = 16; let stack_align = 16;

View File

@@ -13,7 +13,7 @@ use isa::enc_tables::{self as shared_enc_tables, lookup_enclist, Encodings};
use isa::Builder as IsaBuilder; use isa::Builder as IsaBuilder;
use isa::{EncInfo, RegClass, RegInfo, TargetIsa}; use isa::{EncInfo, RegClass, RegInfo, TargetIsa};
use regalloc; use regalloc;
use result; use result::CtonResult;
use std::boxed::Box; use std::boxed::Box;
use std::fmt; use std::fmt;
use target_lexicon::{PointerWidth, Triple}; use target_lexicon::{PointerWidth, Triple};
@@ -129,7 +129,7 @@ impl TargetIsa for Isa {
emit_function(func, binemit::emit_inst, sink) emit_function(func, binemit::emit_inst, sink)
} }
fn prologue_epilogue(&self, func: &mut ir::Function) -> result::CtonResult { fn prologue_epilogue(&self, func: &mut ir::Function) -> CtonResult<()> {
let _tt = timing::prologue_epilogue(); let _tt = timing::prologue_epilogue();
abi::prologue_epilogue(func, self) abi::prologue_epilogue(func, self)
} }

View File

@@ -72,7 +72,7 @@ impl Context {
func: &mut Function, func: &mut Function,
cfg: &ControlFlowGraph, cfg: &ControlFlowGraph,
domtree: &mut DominatorTree, domtree: &mut DominatorTree,
) -> CtonResult { ) -> CtonResult<()> {
let _tt = timing::regalloc(); let _tt = timing::regalloc();
debug_assert!(domtree.is_valid()); debug_assert!(domtree.is_valid());

View File

@@ -32,7 +32,7 @@ pub enum CtonError {
} }
/// A Cretonne compilation result. /// A Cretonne compilation result.
pub type CtonResult = Result<(), CtonError>; pub type CtonResult<T> = Result<T, CtonError>;
impl From<VerifierError> for CtonError { impl From<VerifierError> for CtonError {
fn from(e: VerifierError) -> Self { fn from(e: VerifierError) -> Self {

View File

@@ -2,7 +2,7 @@
use ir::stackslot::{StackOffset, StackSize, StackSlotKind}; use ir::stackslot::{StackOffset, StackSize, StackSlotKind};
use ir::StackSlots; use ir::StackSlots;
use result::CtonError; use result::{CtonError, CtonResult};
use std::cmp::{max, min}; use std::cmp::{max, min};
/// Compute the stack frame layout. /// Compute the stack frame layout.
@@ -15,7 +15,7 @@ use std::cmp::{max, min};
/// Returns the total stack frame size which is also saved in `frame.frame_size`. /// Returns the total stack frame size which is also saved in `frame.frame_size`.
/// ///
/// If the stack frame is too big, returns an `ImplLimitExceeded` error. /// If the stack frame is too big, returns an `ImplLimitExceeded` error.
pub fn layout_stack(frame: &mut StackSlots, alignment: StackSize) -> Result<StackSize, CtonError> { pub fn layout_stack(frame: &mut StackSlots, alignment: StackSize) -> CtonResult<StackSize> {
// Each object and the whole stack frame must fit in 2 GB such that any relative offset within // Each object and the whole stack frame must fit in 2 GB such that any relative offset within
// the frame fits in a `StackOffset`. // the frame fits in a `StackOffset`.
let max_size = StackOffset::max_value() as StackSize; let max_size = StackOffset::max_value() as StackSize;