ruby/lib/ruby_vm/rjit/code_block.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 строки
2.2 KiB
Ruby
Исходник Обычный вид История

2023-03-07 10:15:30 +03:00
module RubyVM::RJIT
2022-12-31 08:34:36 +03:00
class CodeBlock
# @param mem_block [Integer] JIT buffer address
# @param mem_size [Integer] JIT buffer size
2022-12-31 10:42:00 +03:00
# @param outliend [TrueClass,FalseClass] true for outlined CodeBlock
def initialize(mem_block:, mem_size:, outlined: false)
2022-12-31 08:34:36 +03:00
@comments = Hash.new { |h, k| h[k] = [] }
@mem_block = mem_block
@mem_size = mem_size
@write_pos = 0
2022-12-31 10:42:00 +03:00
@outlined = outlined
2022-12-31 08:34:36 +03:00
end
2023-03-07 10:15:30 +03:00
# @param asm [RubyVM::RJIT::Assembler]
def write(asm)
2022-12-31 08:34:36 +03:00
return 0 if @write_pos + asm.size >= @mem_size
start_addr = write_addr
# Write machine code
2023-03-10 22:55:48 +03:00
C.mprotect_write(@mem_block, @mem_size)
@write_pos += asm.assemble(start_addr)
2023-03-10 22:55:48 +03:00
C.mprotect_exec(@mem_block, @mem_size)
2022-12-31 08:34:36 +03:00
end_addr = write_addr
# Convert comment indexes to addresses
asm.comments.each do |index, comments|
@comments[start_addr + index] += comments
end
asm.comments.clear
2023-03-07 10:17:25 +03:00
# Dump disasm if --rjit-dump-disasm
if C.rjit_opts.dump_disasm && start_addr < end_addr
2022-12-31 08:34:36 +03:00
dump_disasm(start_addr, end_addr)
end
start_addr
end
2023-01-02 10:13:43 +03:00
def set_write_addr(addr)
2023-01-01 00:41:32 +03:00
@write_pos = addr - @mem_block
2023-01-02 10:13:43 +03:00
@comments.delete(addr) # TODO: clean up old comments for all the overwritten range?
end
def with_write_addr(addr)
old_write_pos = @write_pos
2023-01-08 00:21:14 +03:00
set_write_addr(addr)
2023-01-01 00:41:32 +03:00
yield
ensure
@write_pos = old_write_pos
end
2022-12-31 08:34:36 +03:00
def write_addr
@mem_block + @write_pos
end
2023-01-08 00:21:14 +03:00
def include?(addr)
(@mem_block...(@mem_block + @mem_size)).include?(addr)
end
def dump_disasm(from, to, io: STDOUT, color: true, test: false)
C.dump_disasm(from, to, test:).each do |address, mnemonic, op_str|
2022-12-31 08:34:36 +03:00
@comments.fetch(address, []).each do |comment|
2023-03-11 00:19:05 +03:00
io.puts colorize(" # #{comment}", bold: true, color:)
2022-12-31 08:34:36 +03:00
end
2023-03-11 00:19:05 +03:00
io.puts colorize(" 0x#{format("%x", address)}: #{mnemonic} #{op_str}", color:)
2022-12-31 08:34:36 +03:00
end
2023-03-11 00:19:05 +03:00
io.puts
2022-12-31 08:34:36 +03:00
end
2023-03-11 00:19:05 +03:00
private
def colorize(text, bold: false, color:)
return text unless color
2022-12-31 10:42:00 +03:00
buf = +''
buf << "\e[1m" if bold
buf << "\e[34m" if @outlined
buf << text
buf << "\e[0m"
buf
end
2022-12-31 08:34:36 +03:00
def bold(text)
"\e[1m#{text}\e[0m"
end
end
end