Class: Ragweed::Rasm::Subprogram

Inherits:
Array show all
Defined in:
lib/ragweed/rasm/isa.rb

Overview

A code fragment. Push instructions into it. You can push Label objects to create jump targets.

Instance Method Summary collapse

Methods included from Array::ArrayExtensions

#to_hash

Instance Method Details

#assembleObject

Patch code offsets into the instructions to replace abstract labels. Produces raw instruction stream.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ragweed/rasm/isa.rb', line 175

def assemble
  patches = {}
  buf = Ragweed::Sbuf.new

  each do |i|
    if i.kind_of? Instruction
      i.locate(buf.size)
      buf.straw i.to_s
    else
      patches[i] = buf.size
    end
  end

  select {|i| i.kind_of? Instruction}.each {|i| i.patch(patches)}
  buf.clear!
  select {|i| i.kind_of? Instruction}.each {|i|
    buf.straw(i.to_s)
  }

  buf.content
end

#disassembleObject

Produce an array of insns. This is pretty much broken, because it doesn’t pre-patch the instructions.



199
200
201
# File 'lib/ragweed/rasm/isa.rb', line 199

def disassemble
  select {|i| i.kind_of? Ragweed::Rasm::Instruction}.map {|i| i.decode}
end

#dump_disassemblyObject



203
204
205
206
207
# File 'lib/ragweed/rasm/isa.rb', line 203

def dump_disassembly
  disassemble.each_with_index do |insn, i|
    puts "#{ i } #{ insn.mnem }"
  end
end