Skip to content

Commit 2b1bbf2

Browse files
committed
Review feedback.
1 parent ae1127d commit 2b1bbf2

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

src/lib.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,28 @@ pub struct Operand {
676676
}
677677

678678
impl Operand {
679+
const VREG_BITS: usize = 32;
680+
const VREG_SHIFT: usize = 0;
681+
const VREG_MASK: u64 = (1 << Self::VREG_BITS) - 1;
682+
683+
const CLASS_BITS: usize = 2;
684+
const CLASS_SHIFT: usize = Self::VREG_SHIFT + Self::VREG_BITS;
685+
const CLASS_MASK: u64 = (1 << Self::CLASS_BITS) - 1;
686+
687+
const POS_BITS: usize = 1;
688+
const POS_SHIFT: usize = Self::CLASS_SHIFT + Self::CLASS_BITS;
689+
const POS_MASK: u64 = (1 << Self::POS_BITS) - 1;
690+
691+
const KIND_BITS: usize = 1;
692+
const KIND_SHIFT: usize = Self::POS_SHIFT + Self::POS_BITS;
693+
const KIND_MASK: u64 = (1 << Self::KIND_BITS) - 1;
694+
695+
const CONSTRAINT_BITS: usize = 7;
696+
const CONSTRAINT_SHIFT: usize = Self::KIND_SHIFT + Self::KIND_BITS;
697+
const CONSTRAINT_MASK: u64 = (1 << Self::CONSTRAINT_BITS) - 1;
698+
699+
const TOTAL_BITS: usize = Self::CONSTRAINT_SHIFT + Self::CONSTRAINT_BITS;
700+
679701
/// Construct a new operand.
680702
#[inline(always)]
681703
pub fn new(
@@ -711,11 +733,11 @@ impl Operand {
711733
let pos_field = pos as u8 as u64;
712734
let kind_field = kind as u8 as u64;
713735
Operand {
714-
bits: vreg.vreg() as u64
715-
| (class_field << 32)
716-
| (pos_field << 34)
717-
| (kind_field << 35)
718-
| (constraint_field << 36),
736+
bits: ((vreg.vreg() as u64) << Self::VREG_SHIFT)
737+
| (class_field << Self::CLASS_SHIFT)
738+
| (pos_field << Self::POS_SHIFT)
739+
| (kind_field << Self::KIND_SHIFT)
740+
| (constraint_field << Self::CONSTRAINT_SHIFT),
719741
}
720742
}
721743

@@ -915,14 +937,14 @@ impl Operand {
915937
/// are used to track dataflow.
916938
#[inline(always)]
917939
pub fn vreg(self) -> VReg {
918-
let vreg_idx = ((self.bits as usize) & VReg::MAX) as usize;
940+
let vreg_idx = ((self.bits >> Self::VREG_SHIFT) & Self::VREG_MASK) as usize;
919941
VReg::new(vreg_idx, self.class())
920942
}
921943

922944
/// Get the register class used by this operand.
923945
#[inline(always)]
924946
pub fn class(self) -> RegClass {
925-
let class_field = (self.bits >> 32) & 3;
947+
let class_field = (self.bits >> Self::CLASS_SHIFT) & Self::CLASS_MASK;
926948
match class_field {
927949
0 => RegClass::Int,
928950
1 => RegClass::Float,
@@ -935,7 +957,7 @@ impl Operand {
935957
/// (read).
936958
#[inline(always)]
937959
pub fn kind(self) -> OperandKind {
938-
let kind_field = (self.bits >> 35) & 1;
960+
let kind_field = (self.bits >> Self::KIND_SHIFT) & Self::KIND_MASK;
939961
match kind_field {
940962
0 => OperandKind::Def,
941963
1 => OperandKind::Use,
@@ -949,7 +971,7 @@ impl Operand {
949971
/// at "after", though there are cases where this is not true.
950972
#[inline(always)]
951973
pub fn pos(self) -> OperandPos {
952-
let pos_field = (self.bits >> 34) & 1;
974+
let pos_field = (self.bits >> Self::POS_SHIFT) & Self::POS_MASK;
953975
match pos_field {
954976
0 => OperandPos::Early,
955977
1 => OperandPos::Late,
@@ -961,7 +983,8 @@ impl Operand {
961983
/// its allocation must fulfill.
962984
#[inline(always)]
963985
pub fn constraint(self) -> OperandConstraint {
964-
let constraint_field = ((self.bits >> 36) as usize) & 0b1111111;
986+
let constraint_field =
987+
((self.bits >> Self::CONSTRAINT_SHIFT) & Self::CONSTRAINT_MASK) as usize;
965988
if constraint_field & 0b1000000 != 0 {
966989
OperandConstraint::FixedReg(PReg::new(constraint_field & 0b0111111, self.class()))
967990
} else if constraint_field & 0b0100000 != 0 {
@@ -999,7 +1022,7 @@ impl Operand {
9991022
/// from `bits()`.
10001023
#[inline(always)]
10011024
pub fn from_bits(bits: u64) -> Self {
1002-
debug_assert!(bits >> 40 <= 4);
1025+
debug_assert_eq!(bits >> Self::TOTAL_BITS, 0);
10031026
Operand { bits }
10041027
}
10051028
}

0 commit comments

Comments
 (0)