Class: SyntaxTree::YARV::Disassembler

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/disassembler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDisassembler

Returns a new instance of Disassembler.



10
11
12
13
14
15
16
# File 'lib/syntax_tree/yarv/disassembler.rb', line 10

def initialize
  @output = StringIO.new
  @queue = []

  @current_prefix = ""
  @current_iseq = nil
end

Instance Attribute Details

#current_iseqObject

Returns the value of attribute current_iseq.



8
9
10
# File 'lib/syntax_tree/yarv/disassembler.rb', line 8

def current_iseq
  @current_iseq
end

#current_prefixObject (readonly)

Returns the value of attribute current_prefix.



7
8
9
# File 'lib/syntax_tree/yarv/disassembler.rb', line 7

def current_prefix
  @current_prefix
end

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/syntax_tree/yarv/disassembler.rb', line 6

def output
  @output
end

#queueObject (readonly)

Returns the value of attribute queue.



6
7
8
# File 'lib/syntax_tree/yarv/disassembler.rb', line 6

def queue
  @queue
end

Instance Method Details

#calldata(value) ⇒ Object

Helpers for various instructions



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/syntax_tree/yarv/disassembler.rb', line 22

def calldata(value)
  flag_names = []
  flag_names << :ARGS_SPLAT if value.flag?(CallData::CALL_ARGS_SPLAT)
  if value.flag?(CallData::CALL_ARGS_BLOCKARG)
    flag_names << :ARGS_BLOCKARG
  end
  flag_names << :FCALL if value.flag?(CallData::CALL_FCALL)
  flag_names << :VCALL if value.flag?(CallData::CALL_VCALL)
  flag_names << :ARGS_SIMPLE if value.flag?(CallData::CALL_ARGS_SIMPLE)
  flag_names << :BLOCKISEQ if value.flag?(CallData::CALL_BLOCKISEQ)
  flag_names << :KWARG if value.flag?(CallData::CALL_KWARG)
  flag_names << :KW_SPLAT if value.flag?(CallData::CALL_KW_SPLAT)
  flag_names << :TAILCALL if value.flag?(CallData::CALL_TAILCALL)
  flag_names << :SUPER if value.flag?(CallData::CALL_SUPER)
  flag_names << :ZSUPER if value.flag?(CallData::CALL_ZSUPER)
  flag_names << :OPT_SEND if value.flag?(CallData::CALL_OPT_SEND)
  flag_names << :KW_SPLAT_MUT if value.flag?(CallData::CALL_KW_SPLAT_MUT)

  parts = []
  parts << "mid:#{value.method}" if value.method
  parts << "argc:#{value.argc}"
  parts << "kw:[#{value.kw_arg.join(", ")}]" if value.kw_arg
  parts << flag_names.join("|") if flag_names.any?

  "<calldata!#{parts.join(", ")}>"
end

#enqueue(iseq) ⇒ Object



49
50
51
# File 'lib/syntax_tree/yarv/disassembler.rb', line 49

def enqueue(iseq)
  queue << iseq
end

#event(name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/syntax_tree/yarv/disassembler.rb', line 53

def event(name)
  case name
  when :RUBY_EVENT_B_CALL
    "Bc"
  when :RUBY_EVENT_B_RETURN
    "Br"
  when :RUBY_EVENT_CALL
    "Ca"
  when :RUBY_EVENT_CLASS
    "Cl"
  when :RUBY_EVENT_END
    "En"
  when :RUBY_EVENT_LINE
    "Li"
  when :RUBY_EVENT_RETURN
    "Re"
  else
    raise "Unknown event: #{name}"
  end
end

#format!Object

Main entrypoint



103
104
105
106
107
108
109
110
# File 'lib/syntax_tree/yarv/disassembler.rb', line 103

def format!
  while (@current_iseq = queue.shift)
    output << "\n" if output.pos > 0
    format_iseq(@current_iseq)
  end

  output.string
end

#inline_storage(cache) ⇒ Object



74
75
76
# File 'lib/syntax_tree/yarv/disassembler.rb', line 74

def inline_storage(cache)
  "<is:#{cache}>"
end

#instruction(name, operands = []) ⇒ Object



78
79
80
# File 'lib/syntax_tree/yarv/disassembler.rb', line 78

def instruction(name, operands = [])
  operands.empty? ? name : "%-38s %s" % [name, operands.join(", ")]
end

#label(value) ⇒ Object



82
83
84
# File 'lib/syntax_tree/yarv/disassembler.rb', line 82

def label(value)
  value.name["label_".length..]
end

#local(index, explicit: nil, implicit: nil) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/syntax_tree/yarv/disassembler.rb', line 86

def local(index, explicit: nil, implicit: nil)
  current = current_iseq
  (explicit || implicit).times { current = current.parent_iseq }

  value = "#{current.local_table.name_at(index)}@#{index}"
  value << ", #{explicit}" if explicit
  value
end

#object(value) ⇒ Object



95
96
97
# File 'lib/syntax_tree/yarv/disassembler.rb', line 95

def object(value)
  value.inspect
end