Class: Rubinius::InstructionDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/compiler/iseq.rb

Overview

Aids in decoding an instruction sequence into information about each instruction

Instance Method Summary collapse

Constructor Details

#initialize(iseq) ⇒ InstructionDecoder

Returns a new instance of InstructionDecoder.



95
96
97
# File 'lib/rubinius/compiler/iseq.rb', line 95

def initialize(iseq)
  @iseq = iseq
end

Instance Method Details

#decode(symbols = false) ⇒ Object

Decodes an InstructionSequence (which is essentially a an array of ints) into an array whose elements are arrays of opcode symbols and 0-2 args, depending on the opcode.



104
105
106
107
108
109
110
111
112
# File 'lib/rubinius/compiler/iseq.rb', line 104

def decode(symbols=false)
  stream = decode_between(0, @iseq.size)

  if symbols
    stream.each { |i| i[0] = i[0].opcode }
  end

  return stream
end

#decode_between(start, fin) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rubinius/compiler/iseq.rb', line 114

def decode_between(start, fin)
  offset = start

  stream = []

  while offset < fin
    inst = @iseq[offset]
    op = InstructionSet[inst]

    case op.arg_count
    when 0
      stream << [op]
    when 1
      stream << [op, @iseq[offset+1]]
    when 2
      stream << [op, @iseq[offset+1], @iseq[offset+2]]
    end

    offset += op.width
  end

  return stream
end

#showObject



138
139
140
141
142
143
144
# File 'lib/rubinius/compiler/iseq.rb', line 138

def show
  ip = 0
  decode.each do |inst|
    puts "%4s: %s" % [ip, inst.join(" ")]
    ip += inst.size
  end
end