rubyx/lib/vm/code.rb

47 lines
1.5 KiB
Ruby
Raw Normal View History

2014-05-03 21:18:04 +02:00
require_relative "values"
2014-05-03 14:13:44 +02:00
module Vm
# Base class for anything that we can assemble
2014-05-03 14:13:44 +02:00
# Derived classes include instructions and data(strings)
# The commonality abstracted here is the length and position
# and the ability to assemble itself into the stream
# All code is position independant once assembled.
# But for jumps and calls two passes are neccessary.
# The first setting the position, the second assembling
2014-05-03 21:18:04 +02:00
class Code < Value
2014-05-05 08:35:40 +02:00
# set the position to zero, will have to reset later
def initialize
2014-05-05 08:35:40 +02:00
@position = 0
end
# the position in the stream. Think of it as an address if you want. The difference is small.
# Especially since we produce _only_ position independant code
# in other words, during assembly the position _must_ be resolved into a pc relative address
# and not used as is
def position
throw "Not set" unless @address
@address
end
# The containing class (assembler/function) call this to tell the instruction/data where it is in the
# stream. During assembly the position is then used to calculate pc relative addresses.
def at address
@address = address
end
# length for this code in bytes
def length
2014-05-05 08:35:40 +02:00
raise "Not implemented #{self}"
end
# so currently the interface passes the io (usually string_io) in for the code to assemble itself.
# this may change as the writing is still done externally (or that will change)
def assemble(io)
throw "Not implemented #{self}"
end
end
end