diff options
author | Kevin Newton <kddnewton@gmail.com> | 2022-07-15 16:14:55 -0400 |
---|---|---|
committer | Takashi Kokubun <takashikkbn@gmail.com> | 2022-08-29 08:47:01 -0700 |
commit | 0da253e72cc80c1dbf8517f5217b59a64ec0f44e (patch) | |
tree | 7eb3cb61dbb2b4b23c8e4da81a6d7bbe40f35fb5 /yjit/src/utils.rs | |
parent | bf7277b518d5ab634ee708f54fbb8735a8eafdbc (diff) | |
download | ruby-0da253e72cc80c1dbf8517f5217b59a64ec0f44e.tar.gz |
Port print_int to the new backend (https://github.com/Shopify/ruby/pull/321)
* Port print_int to the new backend
* Tests for print_int and print_str
Diffstat (limited to 'yjit/src/utils.rs')
-rw-r--r-- | yjit/src/utils.rs | 110 |
1 files changed, 63 insertions, 47 deletions
diff --git a/yjit/src/utils.rs b/yjit/src/utils.rs index 98af604193..5f42ba1fdb 100644 --- a/yjit/src/utils.rs +++ b/yjit/src/utils.rs @@ -105,36 +105,6 @@ pub fn iseq_get_location(iseq: IseqPtr) -> String { s } -#[cfg(test)] -mod tests { - #[test] - fn min_max_preserved_after_cast_to_usize() { - use crate::utils::IntoUsize; - - let min: usize = u64::MIN.as_usize(); - assert_eq!(min, u64::MIN.try_into().unwrap()); - let max: usize = u64::MAX.as_usize(); - assert_eq!(max, u64::MAX.try_into().unwrap()); - - let min: usize = u32::MIN.as_usize(); - assert_eq!(min, u32::MIN.try_into().unwrap()); - let max: usize = u32::MAX.as_usize(); - assert_eq!(max, u32::MAX.try_into().unwrap()); - } - - #[test] - fn test_offset_of() { - #[repr(C)] - struct Foo { - a: u8, - b: u64, - } - - assert_eq!(0, offset_of!(Foo, a), "C99 6.7.2.1p13 says no padding at the front"); - assert_eq!(8, offset_of!(Foo, b), "ABI dependent, but should hold"); - } -} - // TODO: we may want to move this function into yjit.c, maybe add a convenient Rust-side wrapper /* // For debugging. Print the bytecode for an iseq. @@ -163,36 +133,31 @@ macro_rules! c_callable { } pub(crate) use c_callable; -/* -pub fn print_int(cb: &mut CodeBlock, opnd: X86Opnd) { +pub fn print_int(asm: &mut Assembler, opnd: Opnd) { c_callable!{ fn print_int_fn(val: i64) { println!("{}", val); } } - push_regs(cb); + asm.cpush_all(); - match opnd { - X86Opnd::Mem(_) | X86Opnd::Reg(_) => { + let argument = match opnd { + Opnd::Mem(_) | Opnd::Reg(_) | Opnd::InsnOut { .. } => { // Sign-extend the value if necessary - if opnd.num_bits() < 64 { - movsx(cb, C_ARG_REGS[0], opnd); + if opnd.rm_num_bits() < 64 { + asm.load_sext(opnd) } else { - mov(cb, C_ARG_REGS[0], opnd); + opnd } - } - X86Opnd::Imm(_) | X86Opnd::UImm(_) => { - mov(cb, C_ARG_REGS[0], opnd); - } + }, + Opnd::Imm(_) | Opnd::UImm(_) => opnd, _ => unreachable!(), - } + }; - mov(cb, RAX, const_ptr_opnd(print_int_fn as *const u8)); - call(cb, RAX); - pop_regs(cb); + asm.ccall(print_int_fn as *const u8, vec![argument]); + asm.cpop_all(); } -*/ /// Generate code to print a pointer pub fn print_ptr(asm: &mut Assembler, opnd: Opnd) { @@ -251,3 +216,54 @@ pub fn print_str(asm: &mut Assembler, str: &str) { asm.cpop_all(); } + +#[cfg(test)] +mod tests { + use super::*; + use crate::asm::CodeBlock; + + #[test] + fn min_max_preserved_after_cast_to_usize() { + use crate::utils::IntoUsize; + + let min: usize = u64::MIN.as_usize(); + assert_eq!(min, u64::MIN.try_into().unwrap()); + let max: usize = u64::MAX.as_usize(); + assert_eq!(max, u64::MAX.try_into().unwrap()); + + let min: usize = u32::MIN.as_usize(); + assert_eq!(min, u32::MIN.try_into().unwrap()); + let max: usize = u32::MAX.as_usize(); + assert_eq!(max, u32::MAX.try_into().unwrap()); + } + + #[test] + fn test_offset_of() { + #[repr(C)] + struct Foo { + a: u8, + b: u64, + } + + assert_eq!(0, offset_of!(Foo, a), "C99 6.7.2.1p13 says no padding at the front"); + assert_eq!(8, offset_of!(Foo, b), "ABI dependent, but should hold"); + } + + #[test] + fn test_print_int() { + let mut asm = Assembler::new(); + let mut cb = CodeBlock::new_dummy(1024); + + print_int(&mut asm, Opnd::Imm(42)); + asm.compile(&mut cb); + } + + #[test] + fn test_print_str() { + let mut asm = Assembler::new(); + let mut cb = CodeBlock::new_dummy(1024); + + print_str(&mut asm, "Hello, world!"); + asm.compile(&mut cb); + } +} |