Skip to content

Commit

Permalink
Fixed width immediates
Browse files Browse the repository at this point in the history
There are a lot of times when encoding AArch64 instructions that we
need to represent an integer value with a custom fixed width. For
example, the offset for a B instruction is 26 bits, so we store an
i32 on the instruction struct and then mask it when we encode.

We've been doing this masking everywhere, which has worked, but
it's getting a bit copy-pasty all over the place. This commit
centralizes that logic to make sure we stay consistent.
  • Loading branch information
kddnewton committed Aug 26, 2022
1 parent 4edebbc commit 4080a18
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 41 deletions.
2 changes: 2 additions & 0 deletions yjit/src/asm/arm64/arg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ mod condition;
mod sf;
mod shifted_imm;
mod sys_reg;
mod truncate;

pub use bitmask_imm::BitmaskImmediate;
pub use condition::Condition;
pub use sf::Sf;
pub use shifted_imm::ShiftedImmediate;
pub use sys_reg::SystemRegister;
pub use truncate::{truncate_imm, truncate_uimm};
66 changes: 66 additions & 0 deletions yjit/src/asm/arm64/arg/truncate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// There are many instances in AArch64 instruction encoding where you represent
// an integer value with a particular bit width that isn't a power of 2. These
// functions represent truncating those integer values down to the appropriate
// number of bits.

/// Truncate a signed immediate to fit into a compile-time known width. It is
/// assumed before calling this function that the value fits into the correct
/// size. If it doesn't, then this function will panic.
///
/// When the value is positive, this should effectively be a no-op since we're
/// just dropping leading zeroes. When the value is negative we should only be
/// dropping leading ones.
pub fn truncate_imm<T: Into<i32>, const WIDTH: usize>(imm: T) -> u32 {
let value: i32 = imm.into();
let masked = (value as u32) & ((1 << WIDTH) - 1);

// Assert that we didn't drop any bits by truncating.
if value >= 0 {
assert_eq!(value as u32, masked);
} else {
assert_eq!(value as u32, masked | (u32::MAX << WIDTH));
}

masked
}

/// Truncate an unsigned immediate to fit into a compile-time known width. It is
/// assumed before calling this function that the value fits into the correct
/// size. If it doesn't, then this function will panic.
///
/// This should effectively be a no-op since we're just dropping leading zeroes.
pub fn truncate_uimm<T: Into<u32>, const WIDTH: usize>(uimm: T) -> u32 {
let value: u32 = uimm.into();
let masked = (value & ((1 << WIDTH) - 1));

// Assert that we didn't drop any bits by truncating.
assert_eq!(value, masked);

masked
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_truncate_imm_positive() {
let inst = truncate_imm::<i32, 4>(5);
let result: u32 = inst.into();
assert_eq!(0b0101, result);
}

#[test]
fn test_truncate_imm_negative() {
let inst = truncate_imm::<i32, 4>(-5);
let result: u32 = inst.into();
assert_eq!(0b1011, result);
}

#[test]
fn test_truncate_uimm() {
let inst = truncate_uimm::<u32, 4>(5);
let result: u32 = inst.into();
assert_eq!(0b0101, result);
}
}
18 changes: 11 additions & 7 deletions yjit/src/asm/arm64/inst/branch_cond.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::arg::Condition;
use super::super::arg::{Condition, truncate_imm};

/// The struct that represents an A64 conditional branch instruction that can be
/// encoded.
Expand Down Expand Up @@ -31,12 +31,10 @@ const FAMILY: u32 = 0b101;
impl From<BranchCond> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: BranchCond) -> Self {
let imm19 = (inst.imm19 as u32) & ((1 << 19) - 1);

0
| (1 << 30)
| (FAMILY << 26)
| (imm19 << 5)
| (truncate_imm::<_, 19>(inst.imm19) << 5)
| (inst.cond as u32)
}
}
Expand Down Expand Up @@ -66,8 +64,14 @@ mod tests {
}

#[test]
fn test_b_ne_neg() {
let result: u32 = BranchCond::bcond(Condition::NE, -128).into();
assert_eq!(0x54fffc01, result);
fn test_b_eq_max() {
let result: u32 = BranchCond::bcond(Condition::EQ, (1 << 20) - 4).into();
assert_eq!(0x547fffe0, result);
}

#[test]
fn test_b_eq_min() {
let result: u32 = BranchCond::bcond(Condition::EQ, -(1 << 20)).into();
assert_eq!(0x54800000, result);
}
}
14 changes: 7 additions & 7 deletions yjit/src/asm/arm64/inst/call.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::super::arg::truncate_imm;

/// The operation to perform for this instruction.
enum Op {
/// Branch directly, with a hint that this is not a subroutine call or
Expand Down Expand Up @@ -45,12 +47,10 @@ const FAMILY: u32 = 0b101;
impl From<Call> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: Call) -> Self {
let imm26 = (inst.imm26 as u32) & ((1 << 26) - 1);

0
| ((inst.op as u32) << 31)
| (FAMILY << 26)
| imm26
| truncate_imm::<_, 26>(inst.imm26)
}
}

Expand Down Expand Up @@ -92,13 +92,13 @@ mod tests {

#[test]
fn test_b_positive() {
let result: u32 = Call::b(256).into();
assert_eq!(0x14000100, result);
let result: u32 = Call::b((1 << 25) - 1).into();
assert_eq!(0x15ffffff, result);
}

#[test]
fn test_b_negative() {
let result: u32 = Call::b(-256).into();
assert_eq!(0x17ffff00, result);
let result: u32 = Call::b(-(1 << 25)).into();
assert_eq!(0x16000000, result);
}
}
6 changes: 2 additions & 4 deletions yjit/src/asm/arm64/inst/data_reg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::arg::Sf;
use super::super::arg::{Sf, truncate_uimm};

/// The operation being performed by this instruction.
enum Op {
Expand Down Expand Up @@ -129,8 +129,6 @@ const FAMILY: u32 = 0b0101;
impl From<DataReg> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: DataReg) -> Self {
let imm6 = (inst.imm6 as u32) & ((1 << 6) - 1);

0
| ((inst.sf as u32) << 31)
| ((inst.op as u32) << 30)
Expand All @@ -139,7 +137,7 @@ impl From<DataReg> for u32 {
| (1 << 24)
| ((inst.shift as u32) << 22)
| ((inst.rm as u32) << 16)
| (imm6 << 10)
| (truncate_uimm::<_, 6>(inst.imm6) << 10)
| ((inst.rn as u32) << 5)
| inst.rd as u32
}
Expand Down
6 changes: 3 additions & 3 deletions yjit/src/asm/arm64/inst/load_literal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::super::arg::truncate_imm;

/// The size of the operands being operated on.
enum Opc {
Size32 = 0b00,
Expand Down Expand Up @@ -50,13 +52,11 @@ const FAMILY: u32 = 0b0100;
impl From<LoadLiteral> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: LoadLiteral) -> Self {
let imm19 = (inst.imm19 as u32) & ((1 << 19) - 1);

0
| ((inst.opc as u32) << 30)
| (1 << 28)
| (FAMILY << 25)
| (imm19 << 5)
| (truncate_imm::<_, 19>(inst.imm19) << 5)
| (inst.rt as u32)
}
}
Expand Down
6 changes: 3 additions & 3 deletions yjit/src/asm/arm64/inst/load_store.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::super::arg::truncate_imm;

/// The size of the operands being operated on.
enum Size {
Size32 = 0b10,
Expand Down Expand Up @@ -110,14 +112,12 @@ const FAMILY: u32 = 0b0100;
impl From<LoadStore> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: LoadStore) -> Self {
let imm9 = (inst.imm9 as u32) & ((1 << 9) - 1);

0
| ((inst.size as u32) << 30)
| (0b11 << 28)
| (FAMILY << 25)
| ((inst.opc as u32) << 22)
| (imm9 << 12)
| (truncate_imm::<_, 9>(inst.imm9) << 12)
| ((inst.idx as u32) << 10)
| ((inst.rn as u32) << 5)
| (inst.rt as u32)
Expand Down
6 changes: 2 additions & 4 deletions yjit/src/asm/arm64/inst/logical_reg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::arg::Sf;
use super::super::arg::{Sf, truncate_uimm};

/// Whether or not this is a NOT instruction.
enum N {
Expand Down Expand Up @@ -124,16 +124,14 @@ const FAMILY: u32 = 0b0101;
impl From<LogicalReg> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: LogicalReg) -> Self {
let imm6 = (inst.imm6 as u32) & ((1 << 6) - 1);

0
| ((inst.sf as u32) << 31)
| ((inst.opc as u32) << 29)
| (FAMILY << 25)
| ((inst.shift as u32) << 22)
| ((inst.n as u32) << 21)
| ((inst.rm as u32) << 16)
| (imm6 << 10)
| (truncate_uimm::<_, 6>(inst.imm6) << 10)
| ((inst.rn as u32) << 5)
| inst.rd as u32
}
Expand Down
10 changes: 3 additions & 7 deletions yjit/src/asm/arm64/inst/reg_pair.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::super::arg::truncate_imm;

/// The operation to perform for this instruction.
enum Opc {
/// When the registers are 32-bits wide.
Expand Down Expand Up @@ -114,18 +116,12 @@ const FAMILY: u32 = 0b0100;
impl From<RegisterPair> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: RegisterPair) -> Self {
let mut imm7 = (inst.imm7 as u32) & ((1 << 7) - 1);

if inst.imm7 < 0 {
imm7 |= 1 << 6;
}

0
| ((inst.opc as u32) << 30)
| (1 << 29)
| (FAMILY << 25)
| ((inst.index as u32) << 22)
| (imm7 << 15)
| (truncate_imm::<_, 7>(inst.imm7) << 15)
| ((inst.rt2 as u32) << 10)
| ((inst.rn as u32) << 5)
| (inst.rt1 as u32)
Expand Down
9 changes: 3 additions & 6 deletions yjit/src/asm/arm64/inst/sbfm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::arg::Sf;
use super::super::arg::{Sf, truncate_uimm};

/// The struct that represents an A64 signed bitfield move instruction that can
/// be encoded.
Expand Down Expand Up @@ -56,16 +56,13 @@ const FAMILY: u32 = 0b1001;
impl From<SBFM> for u32 {
/// Convert an instruction into a 32-bit value.
fn from(inst: SBFM) -> Self {
let immr = (inst.immr as u32) & ((1 << 6) - 1);
let imms = (inst.imms as u32) & ((1 << 6) - 1);

0
| ((inst.sf as u32) << 31)
| (FAMILY << 25)
| (1 << 24)
| ((inst.n as u32) << 22)
| (immr << 16)
| (imms << 10)
| (truncate_uimm::<_, 6>(inst.immr) << 16)
| (truncate_uimm::<_, 6>(inst.imms) << 10)
| ((inst.rn as u32) << 5)
| inst.rd as u32
}
Expand Down

0 comments on commit 4080a18

Please sign in to comment.