Class: RubyVM::RJIT::CodeBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_vm/rjit/code_block.rb

Instance Method Summary collapse

Constructor Details

#initialize(mem_block:, mem_size:, outlined: false) ⇒ CodeBlock

Returns a new instance of CodeBlock.

Parameters:

  • mem_block (Integer)

    JIT buffer address

  • mem_size (Integer)

    JIT buffer size

  • outliend (TrueClass, FalseClass)

    true for outlined CodeBlock



6
7
8
9
10
11
12
# File 'lib/ruby_vm/rjit/code_block.rb', line 6

def initialize(mem_block:, mem_size:, outlined: false)
  @comments  = Hash.new { |h, k| h[k] = [] } if dump_disasm?
  @mem_block = mem_block
  @mem_size  = mem_size
  @write_pos = 0
  @outlined  = outlined
end

Instance Method Details

#dump_disasm(from, to, io: STDOUT, color: true, test: false) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ruby_vm/rjit/code_block.rb', line 61

def dump_disasm(from, to, io: STDOUT, color: true, test: false)
  C.dump_disasm(from, to, test:).each do |address, mnemonic, op_str|
    @comments.fetch(address, []).each do |comment|
      io.puts colorize("  # #{comment}", bold: true, color:)
    end
    io.puts colorize("  0x#{format("%x", address)}: #{mnemonic} #{op_str}", color:)
  end
  io.puts
end

#include?(addr) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruby_vm/rjit/code_block.rb', line 57

def include?(addr)
  (@mem_block...(@mem_block + @mem_size)).include?(addr)
end

#set_write_addr(addr) ⇒ Object



40
41
42
43
# File 'lib/ruby_vm/rjit/code_block.rb', line 40

def set_write_addr(addr)
  @write_pos = addr - @mem_block
  @comments.delete(addr) if dump_disasm?
end

#with_write_addr(addr) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/ruby_vm/rjit/code_block.rb', line 45

def with_write_addr(addr)
  old_write_pos = @write_pos
  set_write_addr(addr)
  yield
ensure
  @write_pos = old_write_pos
end

#write(asm) ⇒ Object

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_vm/rjit/code_block.rb', line 15

def write(asm)
  return 0 if @write_pos + asm.size >= @mem_size

  start_addr = write_addr

  # Write machine code
  C.mprotect_write(@mem_block, @mem_size)
  @write_pos += asm.assemble(start_addr)
  C.mprotect_exec(@mem_block, @mem_size)

  end_addr = write_addr

  # Convert comment indexes to addresses
  asm.comments.each do |index, comments|
    @comments[start_addr + index] += comments if dump_disasm?
  end
  asm.comments.clear

  # Dump disasm if --rjit-dump-disasm
  if C.rjit_opts.dump_disasm && start_addr < end_addr
    dump_disasm(start_addr, end_addr)
  end
  start_addr
end

#write_addrObject



53
54
55
# File 'lib/ruby_vm/rjit/code_block.rb', line 53

def write_addr
  @mem_block + @write_pos
end