summaryrefslogtreecommitdiff
path: root/ujit.rb
blob: 7c2a6522c9cd3ef951fa6e4ea6376c3c761d1364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module UJIT
  def self.disasm(iseq)
    iseq = RubyVM::InstructionSequence.of(iseq)

    blocks = UJIT.blocks_for(iseq)
    return if blocks.empty?

    str = ""

    cs = UJIT::Disasm.new

    str << iseq.disasm
    str << "\n"

    # Sort the blocks by increasing addresses
    blocks.sort_by(&:address).each_with_index do |block, i|
      str << "== BLOCK #{i+1}/#{blocks.length}: #{block.code.length} BYTES, ISEQ RANGE [#{block.iseq_start_index},#{block.iseq_end_index}[ ".ljust(80, "=")
      str << "\n"

      cs.disasm(block.code, 0).each do |i|
        str << sprintf(
          "  %<address>08X:  %<instruction>s\t%<details>s\n",
          address: block.address + i.address,
          instruction: i.mnemonic,
          details: i.op_str
        )
      end
    end

    block_sizes = blocks.map { |block| block.code.length }
    total_bytes = block_sizes.reduce(0, :+)
    str << "\n"
    str << "Total code size: #{total_bytes} bytes"
    str << "\n"

    str
  end if defined?(Disasm)
end