summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2023-03-10 13:19:05 -0800
committerTakashi Kokubun <takashikkbn@gmail.com>2023-03-10 13:57:52 -0800
commit76808b1ee45db247ad2aad9cc950a3a3a6e888eb (patch)
tree3560cc18ab1c0c3071ccc4c27c892ab9a6911fdc /test/ruby
parent6440d159b31feb495cacb279b204cdb6b54e721f (diff)
RJIT: Start testing Assembler
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/rjit/test_assembler.rb66
1 files changed, 66 insertions, 0 deletions
diff --git a/test/ruby/rjit/test_assembler.rb b/test/ruby/rjit/test_assembler.rb
new file mode 100644
index 0000000000..12d73b2410
--- /dev/null
+++ b/test/ruby/rjit/test_assembler.rb
@@ -0,0 +1,66 @@
+require 'test/unit'
+require_relative '../../lib/jit_support'
+
+return unless JITSupport.rjit_supported?
+return unless RubyVM::RJIT.enabled?
+return unless RubyVM::RJIT::C.HAVE_LIBCAPSTONE
+
+require 'stringio'
+require 'ruby_vm/rjit/assembler'
+
+module RubyVM::RJIT
+ class TestAssembler < Test::Unit::TestCase
+ MEM_SIZE = 16 * 1024
+
+ def setup
+ @mem_block ||= C.mmap(MEM_SIZE)
+ @cb = CodeBlock.new(mem_block: @mem_block, mem_size: MEM_SIZE)
+ end
+
+ def test_add
+ asm = Assembler.new
+ asm.add(:rax, 255)
+ assert_compile(asm, '0x0: add rax, 0xff')
+ end
+
+ def test_jmp
+ asm = Assembler.new
+ label = asm.new_label('label')
+ asm.jmp(label)
+ asm.write_label(label)
+ asm.jmp(label)
+ assert_compile(asm, <<~EOS)
+ 0x0: jmp 0x2
+ 0x2: jmp 0x2
+ EOS
+ end
+
+ private
+
+ def assert_compile(asm, expected)
+ actual = compile(asm)
+ assert_equal expected, actual, "---\n#{actual}---"
+ end
+
+ def compile(asm)
+ start_addr = @cb.write_addr
+ @cb.write(asm)
+ end_addr = @cb.write_addr
+
+ io = StringIO.new
+ @cb.dump_disasm(start_addr, end_addr, io:, color: false)
+ io.seek(0)
+ disasm = io.read
+
+ disasm.gsub!(/^ /, '')
+ disasm.sub!(/\n\z/, '')
+ if disasm.lines.size == 1
+ disasm.rstrip!
+ end
+ (start_addr...end_addr).each do |addr|
+ disasm.gsub!("0x#{addr.to_s(16)}", "0x#{(addr - start_addr).to_s(16)}")
+ end
+ disasm
+ end
+ end
+end