Move duplicated entity code into a macro.
Implement ReservedValue for all the entities.
This commit is contained in:
@@ -20,18 +20,19 @@
|
|||||||
//! format.
|
//! format.
|
||||||
|
|
||||||
use entity_map::EntityRef;
|
use entity_map::EntityRef;
|
||||||
|
use packed_option::ReservedValue;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
use std::u32;
|
use std::u32;
|
||||||
|
|
||||||
/// An opaque reference to an extended basic block in a function.
|
// Implement the common traits for a 32-bit entity reference.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
|
macro_rules! entity_impl {
|
||||||
pub struct Ebb(u32);
|
// Basic traits.
|
||||||
|
($entity:ident) => {
|
||||||
impl EntityRef for Ebb {
|
impl EntityRef for $entity {
|
||||||
fn new(index: usize) -> Self {
|
fn new(index: usize) -> Self {
|
||||||
assert!(index < (u32::MAX as usize));
|
assert!(index < (u32::MAX as usize));
|
||||||
Ebb(index as u32)
|
$entity(index as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index(self) -> usize {
|
fn index(self) -> usize {
|
||||||
@@ -39,6 +40,31 @@ impl EntityRef for Ebb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ReservedValue for $entity {
|
||||||
|
fn reserved_value() -> $entity {
|
||||||
|
$entity(u32::MAX)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Include basic `Display` impl using the given display prefix.
|
||||||
|
// Display an `Ebb` reference as "ebb12".
|
||||||
|
($entity:ident, $display_prefix:expr) => {
|
||||||
|
entity_impl!($entity);
|
||||||
|
|
||||||
|
impl Display for $entity {
|
||||||
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(fmt, "{}{}", $display_prefix, self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An opaque reference to an extended basic block in a function.
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
|
||||||
|
pub struct Ebb(u32);
|
||||||
|
entity_impl!(Ebb, "ebb");
|
||||||
|
|
||||||
impl Ebb {
|
impl Ebb {
|
||||||
/// Create a new EBB reference from its number. This corresponds to the ebbNN representation.
|
/// Create a new EBB reference from its number. This corresponds to the ebbNN representation.
|
||||||
pub fn with_number(n: u32) -> Option<Ebb> {
|
pub fn with_number(n: u32) -> Option<Ebb> {
|
||||||
@@ -46,13 +72,6 @@ impl Ebb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Display an `Ebb` reference as "ebb12".
|
|
||||||
impl Display for Ebb {
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "ebb{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A guaranteed invalid EBB reference.
|
/// A guaranteed invalid EBB reference.
|
||||||
pub const NO_EBB: Ebb = Ebb(u32::MAX);
|
pub const NO_EBB: Ebb = Ebb(u32::MAX);
|
||||||
|
|
||||||
@@ -65,24 +84,7 @@ impl Default for Ebb {
|
|||||||
/// An opaque reference to an instruction in a function.
|
/// An opaque reference to an instruction in a function.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
|
||||||
pub struct Inst(u32);
|
pub struct Inst(u32);
|
||||||
|
entity_impl!(Inst, "inst");
|
||||||
impl EntityRef for Inst {
|
|
||||||
fn new(index: usize) -> Self {
|
|
||||||
assert!(index < (u32::MAX as usize));
|
|
||||||
Inst(index as u32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index(self) -> usize {
|
|
||||||
self.0 as usize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Display an `Inst` reference as "inst7".
|
|
||||||
impl Display for Inst {
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "inst{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A guaranteed invalid instruction reference.
|
/// A guaranteed invalid instruction reference.
|
||||||
pub const NO_INST: Inst = Inst(u32::MAX);
|
pub const NO_INST: Inst = Inst(u32::MAX);
|
||||||
@@ -97,17 +99,7 @@ impl Default for Inst {
|
|||||||
/// An opaque reference to an SSA value.
|
/// An opaque reference to an SSA value.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct Value(u32);
|
pub struct Value(u32);
|
||||||
|
entity_impl!(Value);
|
||||||
impl EntityRef for Value {
|
|
||||||
fn new(index: usize) -> Value {
|
|
||||||
assert!(index < (u32::MAX as usize));
|
|
||||||
Value(index as u32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index(self) -> usize {
|
|
||||||
self.0 as usize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Value references can either reference an instruction directly, or they can refer to the
|
/// Value references can either reference an instruction directly, or they can refer to the
|
||||||
/// extended value table.
|
/// extended value table.
|
||||||
@@ -214,24 +206,7 @@ impl Default for Value {
|
|||||||
/// An opaque reference to a stack slot.
|
/// An opaque reference to a stack slot.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct StackSlot(u32);
|
pub struct StackSlot(u32);
|
||||||
|
entity_impl!(StackSlot, "ss");
|
||||||
impl EntityRef for StackSlot {
|
|
||||||
fn new(index: usize) -> StackSlot {
|
|
||||||
assert!(index < (u32::MAX as usize));
|
|
||||||
StackSlot(index as u32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index(self) -> usize {
|
|
||||||
self.0 as usize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Display a `StackSlot` reference as "ss12".
|
|
||||||
impl Display for StackSlot {
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "ss{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A guaranteed invalid stack slot reference.
|
/// A guaranteed invalid stack slot reference.
|
||||||
pub const NO_STACK_SLOT: StackSlot = StackSlot(u32::MAX);
|
pub const NO_STACK_SLOT: StackSlot = StackSlot(u32::MAX);
|
||||||
@@ -245,24 +220,7 @@ impl Default for StackSlot {
|
|||||||
/// An opaque reference to a jump table.
|
/// An opaque reference to a jump table.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct JumpTable(u32);
|
pub struct JumpTable(u32);
|
||||||
|
entity_impl!(JumpTable, "jt");
|
||||||
impl EntityRef for JumpTable {
|
|
||||||
fn new(index: usize) -> JumpTable {
|
|
||||||
assert!(index < (u32::MAX as usize));
|
|
||||||
JumpTable(index as u32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index(self) -> usize {
|
|
||||||
self.0 as usize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Display a `JumpTable` reference as "jt12".
|
|
||||||
impl Display for JumpTable {
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "jt{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A guaranteed invalid jump table reference.
|
/// A guaranteed invalid jump table reference.
|
||||||
pub const NO_JUMP_TABLE: JumpTable = JumpTable(u32::MAX);
|
pub const NO_JUMP_TABLE: JumpTable = JumpTable(u32::MAX);
|
||||||
@@ -276,24 +234,7 @@ impl Default for JumpTable {
|
|||||||
/// A reference to an external function.
|
/// A reference to an external function.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct FuncRef(u32);
|
pub struct FuncRef(u32);
|
||||||
|
entity_impl!(FuncRef, "fn");
|
||||||
impl EntityRef for FuncRef {
|
|
||||||
fn new(index: usize) -> FuncRef {
|
|
||||||
assert!(index < (u32::MAX as usize));
|
|
||||||
FuncRef(index as u32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index(self) -> usize {
|
|
||||||
self.0 as usize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Display a `FuncRef` reference as "fn12".
|
|
||||||
impl Display for FuncRef {
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "fn{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A guaranteed invalid function reference.
|
/// A guaranteed invalid function reference.
|
||||||
pub const NO_FUNC_REF: FuncRef = FuncRef(u32::MAX);
|
pub const NO_FUNC_REF: FuncRef = FuncRef(u32::MAX);
|
||||||
@@ -307,24 +248,7 @@ impl Default for FuncRef {
|
|||||||
/// A reference to a function signature.
|
/// A reference to a function signature.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct SigRef(u32);
|
pub struct SigRef(u32);
|
||||||
|
entity_impl!(SigRef, "sig");
|
||||||
impl EntityRef for SigRef {
|
|
||||||
fn new(index: usize) -> SigRef {
|
|
||||||
assert!(index < (u32::MAX as usize));
|
|
||||||
SigRef(index as u32)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn index(self) -> usize {
|
|
||||||
self.0 as usize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Display a `SigRef` reference as "sig12".
|
|
||||||
impl Display for SigRef {
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
||||||
write!(fmt, "sig{}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A guaranteed invalid function reference.
|
/// A guaranteed invalid function reference.
|
||||||
pub const NO_SIG_REF: SigRef = SigRef(u32::MAX);
|
pub const NO_SIG_REF: SigRef = SigRef(u32::MAX);
|
||||||
|
|||||||
Reference in New Issue
Block a user