Skip to content

Commit

Permalink
YJIT: fix 32 and 16 bit register store (#6840)
Browse files Browse the repository at this point in the history
* Fix 32 and 16 bit register store in YJIT

Co-Authored-By: Takashi Kokubun <takashikkbn@gmail.com>

* Remove an unnecessary diff

* Reuse an rm_num_bits result

* Use u16::MAX instead

* Update the link

Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>

* Just use sturh for 16 bits

Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 1, 2022
1 parent 01790de commit 06a0c58
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions yjit/src/asm/arm64/inst/load_store.rs
Expand Up @@ -118,6 +118,12 @@ impl LoadStore {
pub fn stur(rt: u8, rn: u8, imm9: i16, num_bits: u8) -> Self {
Self { rt, rn, idx: Index::None, imm9, opc: Opc::STR, size: num_bits.into() }
}

/// STURH (store register, halfword, unscaled)
/// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/STURH--Store-Register-Halfword--unscaled--?lang=en
pub fn sturh(rt: u8, rn: u8, imm9: i16) -> Self {
Self { rt, rn, idx: Index::None, imm9, opc: Opc::STR, size: Size::Size16 }
}
}

/// https://developer.arm.com/documentation/ddi0602/2022-03/Index-by-Encoding/Loads-and-Stores?lang=en
Expand Down
17 changes: 16 additions & 1 deletion yjit/src/asm/arm64/mod.rs
Expand Up @@ -903,7 +903,7 @@ pub fn strh_post(cb: &mut CodeBlock, rt: A64Opnd, rn: A64Opnd) {
pub fn stur(cb: &mut CodeBlock, rt: A64Opnd, rn: A64Opnd) {
let bytes: [u8; 4] = match (rt, rn) {
(A64Opnd::Reg(rt), A64Opnd::Mem(rn)) => {
assert!(rt.num_bits == rn.num_bits, "Expected registers to be the same size");
assert!(rn.num_bits == 32 || rn.num_bits == 64);
assert!(mem_disp_fits_bits(rn.disp), "Expected displacement to be 9 bits or less");

LoadStore::stur(rt.reg_no, rn.base_reg_no, rn.disp as i16, rt.num_bits).into()
Expand All @@ -914,6 +914,21 @@ pub fn stur(cb: &mut CodeBlock, rt: A64Opnd, rn: A64Opnd) {
cb.write_bytes(&bytes);
}

/// STURH - store a value in a register at a memory address
pub fn sturh(cb: &mut CodeBlock, rt: A64Opnd, rn: A64Opnd) {
let bytes: [u8; 4] = match (rt, rn) {
(A64Opnd::Reg(rt), A64Opnd::Mem(rn)) => {
assert!(rn.num_bits == 16);
assert!(mem_disp_fits_bits(rn.disp), "Expected displacement to be 9 bits or less");

LoadStore::sturh(rt.reg_no, rn.base_reg_no, rn.disp as i16).into()
},
_ => panic!("Invalid operand combination to stur instruction.")
};

cb.write_bytes(&bytes);
}

/// SUB - subtract rm from rn, put the result in rd, don't update flags
pub fn sub(cb: &mut CodeBlock, rd: A64Opnd, rn: A64Opnd, rm: A64Opnd) {
let bytes: [u8; 4] = match (rd, rn, rm) {
Expand Down
24 changes: 23 additions & 1 deletion yjit/src/backend/arm64/mod.rs
Expand Up @@ -821,7 +821,11 @@ impl Assembler
// the Arm64 assembler works, the register that is going to
// be stored is first and the address is second. However in
// our IR we have the address first and the register second.
stur(cb, src.into(), dest.into());
match dest.rm_num_bits() {
64 | 32 => stur(cb, src.into(), dest.into()),
16 => sturh(cb, src.into(), dest.into()),
num_bits => panic!("unexpected dest num_bits: {} (src: {:#?}, dest: {:#?})", num_bits, src, dest),
}
},
Insn::Load { opnd, out } |
Insn::LoadInto { opnd, dest: out } => {
Expand Down Expand Up @@ -1378,6 +1382,24 @@ mod tests {
asm.compile_with_num_regs(&mut cb, 2);
}

#[test]
fn test_16_bit_register_store_some_number() {
let (mut asm, mut cb) = setup_asm();

let shape_opnd = Opnd::mem(16, Opnd::Reg(X0_REG), 0);
asm.store(shape_opnd, Opnd::UImm(4097));
asm.compile_with_num_regs(&mut cb, 2);
}

#[test]
fn test_32_bit_register_store_some_number() {
let (mut asm, mut cb) = setup_asm();

let shape_opnd = Opnd::mem(32, Opnd::Reg(X0_REG), 6);
asm.store(shape_opnd, Opnd::UImm(4097));
asm.compile_with_num_regs(&mut cb, 2);
}

#[test]
fn test_emit_xor() {
let (mut asm, mut cb) = setup_asm();
Expand Down

0 comments on commit 06a0c58

Please sign in to comment.