most of the length and assembly stuff fixed

This commit is contained in:
Torsten Ruger 2015-10-25 10:54:19 +02:00
parent 405a6935d4
commit 471329917b
6 changed files with 45 additions and 25 deletions

View File

@ -28,17 +28,17 @@ module Register
@machine.objects.each do |id , objekt|
next unless objekt.is_a? Parfait::Method
# should be fill_to_length (with zeros)
objekt.binary.set_length(objekt.source.byte_length , 0)
objekt.binary.set_length(objekt.source.total_byte_length , 0)
end
#need the initial jump at 0 and then functions
@machine.init.set_position(at)
at += @machine.init.byte_length
at += @machine.init.total_byte_length
at += 8 # thats the padding
# then we make sure we really get the binary codes first
@machine.objects.each do |id , objekt|
next unless objekt.is_a? Parfait::BinaryCode
objekt.set_position at
objekt.position = at
#puts "CODE #{objekt.name} at #{objekt.position}"
at += objekt.word_length
end
@ -50,7 +50,7 @@ module Register
objekt.source.set_position( objekt.binary.position )
end
next if objekt.is_a? Parfait::BinaryCode
objekt.set_position at
objekt.position = at
at += objekt.word_length
end
end
@ -61,6 +61,7 @@ module Register
return try_write
rescue LinkException
# knowing that we fix the problem, we hope to get away with retry.
puts "retry"
retry
end
end
@ -106,23 +107,20 @@ module Register
# and then plonk that binary data into the method.code array
def assemble_binary_method method
stream = StringIO.new
method.source.blocks.each do |block|
block.codes.each do |code|
begin
code.assemble( stream )
rescue => e
puts "Assembly error #{method.name}\n#{Sof.write(method.source.blocks).to_s[0...2000]}"
puts Sof.write(code)
raise e
end
end
puts "Method #{method.source.instructions.to_ac}"
begin
puts "assemble #{method.source.instructions}"
method.source.instructions.assemble_all( stream )
rescue => e
puts "Assembly error #{method.name}\n#{Sof.write(method.source.instructions).to_s[0...2000]}"
raise e
end
method.binary.fill_with 0
index = 1
stream.rewind
#puts "Assembled #{method.name} with length #{stream.length}"
raise "length error #{method.binary.length} != #{method.source.byte_length}" if method.binary.length != method.source.byte_length
raise "length error #{stream.length} != #{method.source.byte_length}" if method.source.byte_length != stream.length
raise "length error #{method.binary.length} != #{method.source.total_byte_length}" if method.binary.length != method.source.total_byte_length
raise "length error #{stream.length} != #{method.source.total_byte_length}" if method.source.total_byte_length != stream.length
stream.each_byte do |b|
method.binary.set_char(index , b )
index = index + 1
@ -229,6 +227,7 @@ module Register
@stream.write_uint8(0)
end
after = stream_position
before - after # shut up the linter
#puts "padded #{length.to_s(16)} with #{pad.to_s(16)} stream #{before}/#{after}"
end

View File

@ -81,9 +81,19 @@ module Register
raise "Abstract called on #{self}"
end
def assemble_all io , labels = []
self.assemble(io)
self.next.assemble_all(io, labels) if self.next
end
def assemble io
raise "Abstract called on #{self}"
end
def total_byte_length labels = []
ret = self.byte_length
ret += self.next.total_byte_length(labels) if self.next
#puts "#{self.class.name} return #{ret}"
ret
end

View File

@ -27,8 +27,9 @@ module Register
end
def total_byte_length labels = []
ret = super
ret += label.total_byte_length(labels) if self.label
ret = super(labels)
ret += self.label.total_byte_length(labels) if self.label
#puts "#{self.class.name} return #{ret}"
ret
end
@ -38,6 +39,12 @@ module Register
super(position,labels)
end
def assemble_all io , labels = []
self.assemble(io)
self.label.assemble_all(io,labels) if self.label
self.next.assemble_all(io, labels) if self.next
end
end
class IsZero < Branch

View File

@ -32,14 +32,21 @@ module Register
ret
end
def byte_length
0
def assemble io
end
def assemble_all io , labels = []
return if labels.include?(self)
labels << self
self.next.assemble_all(io,labels)
end
def total_byte_length labels = []
return 0 if labels.include?(self)
labels << self
self.next.length(labels)
ret = self.next.total_byte_length(labels)
#puts "#{self.class.name} return #{ret}"
ret
end
# labels have the same position as their next

View File

@ -33,10 +33,8 @@ module Register
end
methods.each do |method|
instruction = method.instructions
puts "instruction #{instruction.to_ac}" #if instruction.is_a?(Label) and instruction.name == "Method_main"
while instruction.next
nekst = instruction.next
puts "translate #{nekst}"
t = translator.translate(nekst) # returning nil means no replace
if t
nekst = t.last
@ -44,7 +42,6 @@ module Register
end
instruction = nekst
end
puts "instruction #{method.instructions.to_ac}" #if method.instructions.is_a?(Label) and method.instructions.name == "Method_main"
end
label = @init.next
@init = translator.translate( @init)

View File

@ -83,7 +83,7 @@ module Register
self
end
def byte_length
def total_byte_length
@instructions.total_byte_length
end