Class: Cfg2asm::CFG::CFGParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cfg2asm/cfg/cfg_parser.rb

Overview

A parser for CFG files.

Instance Method Summary collapse

Constructor Details

#initialize(out, file) ⇒ CFGParser

Returns a new instance of CFGParser.



12
13
14
15
16
17
18
19
20
21
# File 'lib/cfg2asm/cfg/cfg_parser.rb', line 12

def initialize(out, file)
  @out = out
  data = File.read(file, encoding: Encoding::ASCII_8BIT)
  if data[0..1].bytes == [0x1f, 0x8b]
    data = Zlib.gunzip(data)
  end
  @reader = StringIO.new(data)
  @state = :any
  @cfg_name = nil
end

Instance Method Details

#read_nmethodObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cfg2asm/cfg/cfg_parser.rb', line 45

def read_nmethod
  raise unless @state == :any

  arch = nil
  arch_width = nil
  code = nil
  comments = []
  raise unless @reader.readline == "begin_nmethod\n"

  loop do
    line = @reader.readline("\n")
    case line
    when /  Platform (.*) (.*)  <\|\|@\n/
      arch = Regexp.last_match(1)
      arch_width = Regexp.last_match(2)
    when /  HexCode (.*) (.*)  <\|\|@\n/
      base = Regexp.last_match(1).to_i(16)
      code = [Regexp.last_match(2)].pack('H*')
      raise if arch.nil? || arch_width.nil?

      code = Code.new(arch, arch_width, base, code)
    when /  Comment (\d*) (.*)  <\|\|@\n/
      offset = Regexp.last_match(1).to_i
      comment = Regexp.last_match(2)
      comments.push Comment.new(offset, comment)
    when "  <<<HexCodeFile\n"
      next
    when "  HexCodeFile>>> <|@\n"
      next
    when "end_nmethod\n"
      break
    when /  (.*)  <\|\|@\n/
      offset = -1
      comment = Regexp.last_match(1)
      comments.push Comment.new(offset, comment)
    when /  (.*)\n/
      offset = -1
      comment = Regexp.last_match(1)
      comments.push Comment.new(offset, comment)
    else
      # In case anything was missed
      raise 'There is currently no case for this line. Please open an issue so it can be addressed.'
    end
  end
  NMethod.new(code, comments)
end

#skip_over_cfg(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cfg2asm/cfg/cfg_parser.rb', line 23

def skip_over_cfg(name)
  loop do
    line = @reader.readline("\n")
    case line
    when "begin_cfg\n"
      @state = :cfg
      @cfg_name = nil
    when /  name "(.*)"\n/
      if @state == :cfg
        @cfg_name = Regexp.last_match(1)
      end
    when "end_cfg\n"
      raise unless @state == :cfg

      @state = :any
      break if @cfg_name == name
    else
      next
    end
  end
end