From fd04e1b4dbbb0dae130f3de79d69ca94ecdf883e Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sun, 11 Dec 2022 21:42:25 -0800 Subject: Implement a no-op JIT compiler --- lib/mjit/x86_64/assembler.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/mjit/x86_64/assembler.rb (limited to 'lib/mjit') diff --git a/lib/mjit/x86_64/assembler.rb b/lib/mjit/x86_64/assembler.rb new file mode 100644 index 0000000000..14f414b33b --- /dev/null +++ b/lib/mjit/x86_64/assembler.rb @@ -0,0 +1,35 @@ +class RubyVM::MJIT::Assembler + ByteWriter = RubyVM::MJIT::CType::Immediate.parse('char') + + def initialize + @bytes = [] + end + + def compile(compiler) + RubyVM::MJIT::C.mjit_mark_writable + write_bytes(compiler.write_addr, @bytes) + RubyVM::MJIT::C.mjit_mark_executable + + compiler.write_pos += @bytes.size + @bytes.clear + end + + def mov(_reg, val) + @bytes.push(0xb8, val, 0x00, 0x00, 0x00) + end + + def ret + @bytes.push(0xc3) + end + + private + + def write_bytes(addr, bytes) + writer = ByteWriter.new(addr) + # If you pack bytes containing \x00, Ruby fails to recognize bytes after \x00. + # So writing byte by byte to avoid hitting that situation. + bytes.each_with_index do |byte, index| + writer[index] = byte + end + end +end -- cgit v1.2.3