Skip to content

Commit

Permalink
Add TargetIsa::as_any for downcasting to specific ISA implementations
Browse files Browse the repository at this point in the history
This is necessary when we would like to check specific ISA flags, e.g.
  • Loading branch information
abrown committed May 27, 2020
1 parent 4488980 commit 849d71b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/arm32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa};
use crate::regalloc;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::any::Any;
use core::fmt;
use target_lexicon::{Architecture, Triple};

Expand Down Expand Up @@ -135,6 +136,10 @@ impl TargetIsa for Isa {
fn unsigned_sub_overflow_condition(&self) -> ir::condcodes::IntCC {
ir::condcodes::IntCC::UnsignedGreaterThanOrEqual
}

fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
}

impl fmt::Display for Isa {
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use crate::settings::SetResult;
use crate::timing;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::any::Any;
use core::fmt;
use core::fmt::{Debug, Formatter};
use target_lexicon::{triple, Architecture, PointerWidth, Triple};
Expand Down Expand Up @@ -422,6 +423,10 @@ pub trait TargetIsa: fmt::Display + Send + Sync {
fn get_mach_backend(&self) -> Option<&dyn MachBackend> {
None
}

/// Return an [Any] reference for downcasting to the ISA-specific implementation of this trait
/// with `isa.as_any().downcast_ref::<isa::foo::Isa>()`.
fn as_any(&self) -> &dyn Any;
}

impl Debug for &dyn TargetIsa {
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/riscv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa};
use crate::regalloc;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::any::Any;
use core::fmt;
use target_lexicon::{PointerWidth, Triple};

Expand Down Expand Up @@ -130,6 +131,10 @@ impl TargetIsa for Isa {
fn unsigned_sub_overflow_condition(&self) -> ir::condcodes::IntCC {
unimplemented!()
}

fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
}

#[cfg(test)]
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/isa/x86/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::result::CodegenResult;
use crate::timing;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::any::Any;
use core::fmt;
use target_lexicon::{PointerWidth, Triple};

Expand Down Expand Up @@ -184,6 +185,10 @@ impl TargetIsa for Isa {
fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
Some(unwind::systemv::create_cie())
}

fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
}

impl fmt::Display for Isa {
Expand Down
5 changes: 5 additions & 0 deletions cranelift/codegen/src/machinst/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::settings::Flags;
#[cfg(feature = "testing_hooks")]
use crate::regalloc::RegDiversions;

use core::any::Any;
use std::borrow::Cow;
use std::fmt;
use target_lexicon::Triple;
Expand Down Expand Up @@ -127,4 +128,8 @@ impl TargetIsa for TargetIsaAdapter {
fn unsigned_sub_overflow_condition(&self) -> ir::condcodes::IntCC {
self.backend.unsigned_sub_overflow_condition()
}

fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
}

0 comments on commit 849d71b

Please sign in to comment.