2014-08-22 16:40:09 +02:00
|
|
|
module Register
|
2014-04-25 11:28:23 +02:00
|
|
|
# Base class for anything that we can assemble
|
2014-05-03 14:13:44 +02:00
|
|
|
|
2014-05-22 13:18:22 +02:00
|
|
|
# Derived classes include instructions and constants(data)
|
2014-04-25 11:28:23 +02:00
|
|
|
|
|
|
|
# The commonality abstracted here is the length and position
|
2014-05-22 13:18:22 +02:00
|
|
|
# and the ability to assemble itself into the stream(io)
|
2014-04-25 11:28:23 +02:00
|
|
|
|
|
|
|
# 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-05 10:03:43 +02:00
|
|
|
class Code
|
2014-04-25 11:28:23 +02:00
|
|
|
|
2014-05-21 11:47:40 +02:00
|
|
|
def class_for clazz
|
2014-05-21 18:43:46 +02:00
|
|
|
RegisterMachine.instance.class_for(clazz)
|
2014-05-21 11:47:40 +02:00
|
|
|
end
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
# set the position to zero, will have to reset later
|
2014-04-25 11:28:23 +02:00
|
|
|
def initialize
|
2014-05-05 23:12:04 +02:00
|
|
|
@position = 0
|
2014-04-25 11:28:23 +02:00
|
|
|
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
|
2014-05-05 23:12:04 +02:00
|
|
|
@position
|
2014-04-25 11:28:23 +02:00
|
|
|
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.
|
2014-05-05 10:03:43 +02:00
|
|
|
def link_at address , context
|
2014-05-05 23:12:04 +02:00
|
|
|
@position = address
|
2014-04-25 11:28:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# length for this code in bytes
|
|
|
|
def length
|
2014-05-05 23:12:04 +02:00
|
|
|
raise "Not implemented #{inspect}"
|
2014-04-25 11:28:23 +02:00
|
|
|
end
|
|
|
|
|
2014-05-22 13:18:22 +02:00
|
|
|
# we pass the io (usually string_io) in for the code to assemble itself.
|
2014-04-25 11:28:23 +02:00
|
|
|
def assemble(io)
|
2014-05-05 14:59:29 +02:00
|
|
|
raise "Not implemented #{self.inspect}"
|
2014-04-25 11:28:23 +02:00
|
|
|
end
|
2014-06-07 16:59:44 +02:00
|
|
|
|
2014-04-25 11:28:23 +02:00
|
|
|
end
|
|
|
|
end
|