Skip to content

Commit 0bc2435

Browse files
committed
ZJIT: Restore most x64 backend tests
1 parent f45aa15 commit 0bc2435

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

zjit/src/asm/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::BTreeMap;
2-
//use std::fmt;
2+
use std::fmt;
33
use std::rc::Rc;
44
use std::cell::RefCell;
55
use std::mem;
@@ -260,6 +260,18 @@ impl CodeBlock {
260260
}
261261
}
262262

263+
/// Produce hex string output from the bytes in a code block
264+
impl fmt::LowerHex for CodeBlock {
265+
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
266+
for pos in 0..self.write_pos {
267+
let mem_block = &*self.mem_block.borrow();
268+
let byte = unsafe { mem_block.start_ptr().raw_ptr(mem_block).add(pos).read() };
269+
fmtr.write_fmt(format_args!("{:02x}", byte))?;
270+
}
271+
Ok(())
272+
}
273+
}
274+
263275
#[cfg(test)]
264276
impl CodeBlock {
265277
/// Stubbed CodeBlock for testing. Can't execute generated code.

zjit/src/assertions.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// Assert that CodeBlock has the code specified with hex. In addition, if tested with
2+
/// `cargo test --all-features`, it also checks it generates the specified disasm.
3+
#[cfg(test)]
4+
macro_rules! assert_disasm {
5+
($cb:expr, $hex:expr, $disasm:expr) => {
6+
#[cfg(feature = "disasm")]
7+
{
8+
use $crate::disasm::disasm_addr_range;
9+
use $crate::cruby::unindent;
10+
let disasm = disasm_addr_range(
11+
&$cb,
12+
$cb.get_ptr(0).raw_addr(&$cb),
13+
$cb.get_write_ptr().raw_addr(&$cb),
14+
);
15+
assert_eq!(unindent(&disasm, false), unindent(&$disasm, true));
16+
}
17+
assert_eq!(format!("{:x}", $cb), $hex);
18+
};
19+
}
20+
#[cfg(test)]
21+
pub(crate) use assert_disasm;

zjit/src/backend/lir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,15 @@ impl Assembler
17511751
ret
17521752
}
17531753

1754+
/// Compile with a limited number of registers. Used only for unit tests.
1755+
#[cfg(test)]
1756+
pub fn compile_with_num_regs(self, cb: &mut CodeBlock, num_regs: usize) -> (CodePtr, Vec<u32>)
1757+
{
1758+
let mut alloc_regs = Self::get_alloc_regs();
1759+
let alloc_regs = alloc_regs.drain(0..num_regs).collect();
1760+
self.compile_with_regs(cb, alloc_regs).unwrap()
1761+
}
1762+
17541763
/// Compile Target::SideExit and convert it into Target::CodePtr for all instructions
17551764
#[must_use]
17561765
pub fn compile_side_exits(&mut self) -> Option<()> {

zjit/src/backend/x86_64/mod.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -859,20 +859,17 @@ impl Assembler
859859
}
860860
}
861861

862-
/*
863862
#[cfg(test)]
864863
mod tests {
865-
use crate::disasm::assert_disasm;
866-
#[cfg(feature = "disasm")]
867-
use crate::disasm::{unindent, disasm_addr_range};
868-
864+
use crate::assertions::assert_disasm;
869865
use super::*;
870866

871867
fn setup_asm() -> (Assembler, CodeBlock) {
872-
(Assembler::new(0), CodeBlock::new_dummy(1024))
868+
(Assembler::new(), CodeBlock::new_dummy())
873869
}
874870

875871
#[test]
872+
#[ignore]
876873
fn test_emit_add_lt_32_bits() {
877874
let (mut asm, mut cb) = setup_asm();
878875

@@ -883,6 +880,7 @@ mod tests {
883880
}
884881

885882
#[test]
883+
#[ignore]
886884
fn test_emit_add_gt_32_bits() {
887885
let (mut asm, mut cb) = setup_asm();
888886

@@ -893,6 +891,7 @@ mod tests {
893891
}
894892

895893
#[test]
894+
#[ignore]
896895
fn test_emit_and_lt_32_bits() {
897896
let (mut asm, mut cb) = setup_asm();
898897

@@ -903,6 +902,7 @@ mod tests {
903902
}
904903

905904
#[test]
905+
#[ignore]
906906
fn test_emit_and_gt_32_bits() {
907907
let (mut asm, mut cb) = setup_asm();
908908

@@ -957,6 +957,7 @@ mod tests {
957957
}
958958

959959
#[test]
960+
#[ignore]
960961
fn test_emit_or_lt_32_bits() {
961962
let (mut asm, mut cb) = setup_asm();
962963

@@ -967,6 +968,7 @@ mod tests {
967968
}
968969

969970
#[test]
971+
#[ignore]
970972
fn test_emit_or_gt_32_bits() {
971973
let (mut asm, mut cb) = setup_asm();
972974

@@ -977,6 +979,7 @@ mod tests {
977979
}
978980

979981
#[test]
982+
#[ignore]
980983
fn test_emit_sub_lt_32_bits() {
981984
let (mut asm, mut cb) = setup_asm();
982985

@@ -987,6 +990,7 @@ mod tests {
987990
}
988991

989992
#[test]
993+
#[ignore]
990994
fn test_emit_sub_gt_32_bits() {
991995
let (mut asm, mut cb) = setup_asm();
992996

@@ -1017,6 +1021,7 @@ mod tests {
10171021
}
10181022

10191023
#[test]
1024+
#[ignore]
10201025
fn test_emit_xor_lt_32_bits() {
10211026
let (mut asm, mut cb) = setup_asm();
10221027

@@ -1027,6 +1032,7 @@ mod tests {
10271032
}
10281033

10291034
#[test]
1035+
#[ignore]
10301036
fn test_emit_xor_gt_32_bits() {
10311037
let (mut asm, mut cb) = setup_asm();
10321038

@@ -1050,6 +1056,7 @@ mod tests {
10501056
}
10511057

10521058
#[test]
1059+
#[ignore]
10531060
fn test_merge_lea_mem() {
10541061
let (mut asm, mut cb) = setup_asm();
10551062

@@ -1064,6 +1071,7 @@ mod tests {
10641071
}
10651072

10661073
#[test]
1074+
#[ignore]
10671075
fn test_replace_cmp_0() {
10681076
let (mut asm, mut cb) = setup_asm();
10691077

@@ -1216,6 +1224,7 @@ mod tests {
12161224
}
12171225

12181226
#[test]
1227+
#[ignore]
12191228
fn test_reorder_c_args_with_insn_out() {
12201229
let (mut asm, mut cb) = setup_asm();
12211230

@@ -1259,15 +1268,16 @@ mod tests {
12591268

12601269
asm.compile_with_num_regs(&mut cb, 1);
12611270

1262-
assert_disasm!(cb, "48837b1001b804000000480f4f03488903", {"
1271+
assert_disasm!(cb, "48837b1001bf04000000480f4f3b48893b", {"
12631272
0x0: cmp qword ptr [rbx + 0x10], 1
1264-
0x5: mov eax, 4
1265-
0xa: cmovg rax, qword ptr [rbx]
1266-
0xe: mov qword ptr [rbx], rax
1273+
0x5: mov edi, 4
1274+
0xa: cmovg rdi, qword ptr [rbx]
1275+
0xe: mov qword ptr [rbx], rdi
12671276
"});
12681277
}
12691278

12701279
#[test]
1280+
#[ignore]
12711281
fn test_csel_split() {
12721282
let (mut asm, mut cb) = setup_asm();
12731283

@@ -1285,5 +1295,3 @@ mod tests {
12851295
"});
12861296
}
12871297
}
1288-
1289-
*/

zjit/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ mod disasm;
2121
mod options;
2222
mod profile;
2323
mod invariants;
24+
#[cfg(test)]
25+
mod assertions;

0 commit comments

Comments
 (0)