2014-04-19 22:25:46 +02:00
|
|
|
|
2014-04-14 17:09:56 +02:00
|
|
|
module Asm
|
|
|
|
ERRSTR_NUMERIC_TOO_LARGE = 'cannot fit numeric literal argument in operand'
|
|
|
|
ERRSTR_INVALID_ARG = 'invalid operand argument'
|
|
|
|
|
|
|
|
class Assembler
|
|
|
|
def initialize
|
|
|
|
@objects = []
|
2014-04-21 20:38:39 +02:00
|
|
|
@position = -1 # marks not set
|
2014-04-14 17:09:56 +02:00
|
|
|
@label_objects = []
|
|
|
|
@relocations = []
|
|
|
|
end
|
2014-04-21 20:38:39 +02:00
|
|
|
attr_reader :relocations, :objects , :position
|
2014-04-14 17:09:56 +02:00
|
|
|
|
|
|
|
def add_object(obj)
|
2014-04-21 20:21:45 +02:00
|
|
|
obj.at(@position)
|
|
|
|
@position += obj.length
|
2014-04-14 17:09:56 +02:00
|
|
|
@objects << obj
|
|
|
|
end
|
2014-04-21 20:21:45 +02:00
|
|
|
|
2014-04-14 17:09:56 +02:00
|
|
|
|
|
|
|
def assemble(io)
|
|
|
|
@objects.each do |obj|
|
|
|
|
obj.assemble io, self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-04-21 20:38:39 +02:00
|
|
|
|
|
|
|
# class Relocation
|
|
|
|
# def initialize(pos, label, type, handler)
|
|
|
|
# @position = pos
|
|
|
|
# @label = label
|
|
|
|
# @type = type
|
|
|
|
# @handler = handler
|
|
|
|
# end
|
|
|
|
# attr_reader :position, :label, :type, :handler
|
|
|
|
# end
|
|
|
|
|
|
|
|
#old assemble function
|
|
|
|
#def assemble(io)
|
|
|
|
# @objects.each do |obj|
|
|
|
|
# obj.assemble io, self
|
|
|
|
# end
|
|
|
|
# @relocations.delete_if do |reloc|
|
|
|
|
# io.seek reloc.position
|
|
|
|
# #puts "reloc #{reloc.inspect}"
|
|
|
|
# if (reloc.label.extern?)
|
|
|
|
# reloc.handler.call(io, io.tell, reloc.type)
|
|
|
|
# else
|
|
|
|
# reloc.handler.call(io, reloc.label.address, reloc.type)
|
|
|
|
# end
|
|
|
|
# not reloc.label.extern?
|
|
|
|
#end
|
|
|
|
#end
|
|
|
|
#def add_relocation(*args)
|
|
|
|
# reloc = Asm::Relocation.new(*args)
|
|
|
|
# #raise "reloc #{reloc.inspect}"
|
|
|
|
# @relocations << reloc
|
|
|
|
#end
|
|
|
|
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
|
|
|
|