position filling after the padding changes, works again

This commit is contained in:
Torsten Ruger 2015-11-04 10:34:58 +02:00
parent c87682b77f
commit 2236065d02

View File

@ -10,10 +10,6 @@ module Register
class Assembler class Assembler
include Padding include Padding
TYPE_REF = 0
TYPE_INT = 1
TYPE_BITS = 4
TYPE_LENGTH = 6
def initialize machine def initialize machine
@machine = machine @machine = machine
@ -31,17 +27,17 @@ module Register
next unless objekt.is_a? Parfait::Method next unless objekt.is_a? Parfait::Method
objekt.binary.position = at objekt.binary.position = at
objekt.instructions.set_position at objekt.instructions.set_position at
#puts "CODE #{objekt.name} at #{objekt.position}"
len = objekt.instructions.total_byte_length len = objekt.instructions.total_byte_length
puts "CODE #{objekt.name} at #{objekt.binary.position} len: #{len}"
objekt.binary.set_length(len/4) objekt.binary.set_length(len/4)
at += len at += objekt.binary.padded_length
end end
# and then everything else # and then everything else
@machine.objects.each do | id , objekt| @machine.objects.each do | id , objekt|
next if objekt.is_a? Register::Label # will get assembled as method.instructions next if objekt.is_a? Register::Label # will get assembled as method.instructions
next if objekt.is_a? Parfait::BinaryCode next if objekt.is_a? Parfait::BinaryCode
objekt.position = at objekt.position = at
at += objekt.word_length at += objekt.padded_length
end end
end end
@ -62,7 +58,8 @@ module Register
all = @machine.objects.values.sort{|a,b| a.position <=> b.position} all = @machine.objects.values.sort{|a,b| a.position <=> b.position}
# debugging loop accesses all positions to force an error if it's not set # debugging loop accesses all positions to force an error if it's not set
all.each do |objekt| all.each do |objekt|
#puts "Linked #{objekt.class}(#{objekt.object_id.to_s(16)}) at #{objekt.position.to_s(16)} / #{objekt.word_length.to_s(16)}" next if objekt.is_a?(Register::Label)
puts "Linked #{objekt.class}(#{objekt.object_id}) at #{objekt.position} / #{objekt.padded_length}"
objekt.position objekt.position
end end
# first we need to create the binary code for the methods # first we need to create the binary code for the methods
@ -84,10 +81,10 @@ module Register
# and then the rest of the object machine # and then the rest of the object machine
@machine.objects.each do | id, objekt| @machine.objects.each do | id, objekt|
next if objekt.is_a? Parfait::BinaryCode next if objekt.is_a? Parfait::BinaryCode
next if object.is_a? Register::Label # ignore next if objekt.is_a? Register::Label # ignore
write_any( objekt ) write_any( objekt )
end end
#puts "Assembled #{stream_position} bytes" puts "Assembled #{stream_position} bytes"
return @stream.string return @stream.string
end end
@ -105,55 +102,54 @@ module Register
end end
index = 1 index = 1
stream.rewind stream.rewind
#puts "Assembled #{method.name} with length #{stream.length}" puts "Assembled #{method.name} with length #{stream.length}"
raise "length error #{method.binary.get_length} != #{method.instructions.total_byte_length}" if method.binary.get_length*4 != method.instructions.total_byte_length raise "length error #{method.binary.get_length} != #{method.instructions.total_byte_length}" if method.binary.get_length*4 != method.instructions.total_byte_length
raise "length error #{stream.length} != #{method.instructions.total_byte_length}" if method.instructions.total_byte_length != stream.length raise "length error #{stream.length} != #{method.instructions.total_byte_length}" if method.instructions.total_byte_length != stream.length
stream.each_byte do |b| stream.each_byte do |b|
method.binary.set(index , b ) method.binary.set((index - 1) / 4 + 1 , b )
index = index + 1 index = index + 1
end end
end end
def write_any obj def write_any obj
#puts "Assemble #{obj.class}(#{obj.object_id.to_s(16)}) at stream #{stream_position} pos:#{obj.position.to_s(16)} , len:#{obj.word_length.to_s(16)}" puts "Assemble #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
if @stream.length != obj.position if @stream.length != obj.position
raise "Assemble #{obj.class} #{obj.object_id.to_s(16)} at #{stream_position} not #{obj.position.to_s(16)}" raise "Assemble #{obj.class} #{obj.object_id} at #{stream_position} not #{obj.position}"
end end
if obj.is_a?(Parfait::Word) or obj.is_a?(Symbol) if obj.is_a?(Parfait::Word) or obj.is_a?(Symbol)
write_String obj write_String obj
else else
write_object obj write_object obj
end end
#puts "Assemble #{obj.class}(#{obj.object_id.to_s(16)}) at stream #{stream_position} pos:#{obj.position.to_s(16)} , len:#{obj.word_length.to_s(16)}" puts "Assemble #{obj.class}(#{obj.object_id}) at stream #{stream_position} pos:#{obj.position} , len:#{obj.padded_length}"
if @stream.length != obj.position
raise "Assemble #{obj.class} #{obj.object_id.to_s(16)} at #{stream_position} not #{obj.position.to_s(16)}"
end
obj.position obj.position
end end
# write type and layout of the instance, and the variables that are passed # write type and layout of the instance, and the variables that are passed
# variables ar values, ie int or refs. For refs the object needs to save the object first # variables ar values, ie int or refs. For refs the object needs to save the object first
def write_object( object ) def write_object( object )
puts "Write #{object.class}" puts "Write #{object.class} #{object.inspect}"
unless @machine.objects.has_key? object.object_id unless @machine.objects.has_key? object.object_id
raise "Object(#{object.object_id}) not linked #{object.inspect}" raise "Object(#{object.object_id}) not linked #{object.inspect}"
end end
layout = object.get_layout written = 0
write_ref_for(layout ) object.get_instance_variables.each do |var|
layout.each do |var| inst = object.get_instance_variable(var)
inst = object.instance_variable_get "@#{var}".to_sym
#puts "Nil for #{object.class}.#{var}" unless inst #puts "Nil for #{object.class}.#{var}" unless inst
write_ref_for(inst) write_ref_for(inst)
written += 4
end end
puts "layout length=#{layout.get_length.to_s(16)} mem_len=#{layout.word_length.to_s(16)}" lay_len = written
l = layout.get_length puts "instances=#{object.get_instance_variables.inspect} mem_len=#{object.padded_length}"
if( object.is_a? Parfait::Indexed) if( object.is_a? Parfait::Indexed)
object.each do |inst| object.each do |inst|
write_ref_for(inst) write_ref_for(inst)
l += 4 written += 4
end end
end end
pad_after( l / 4 ) puts "layout #{lay_len} , total #{written} (array #{written - lay_len})"
puts "Len = #{object.get_length} , inst = #{object.get_layout.instance_length}" if object.is_a? Parfait::Layout
pad_after( written )
object.position object.position
end end
@ -164,15 +160,11 @@ module Register
def write_String( string ) def write_String( string )
str = string.to_string if string.is_a? Parfait::Word str = string.to_string if string.is_a? Parfait::Word
str = string.to_s if string.is_a? Symbol str = string.to_s if string.is_a? Symbol
word = (str.length + 7) / 32 # all object are multiple of 8 words (7 for header) puts "String is #{string} at #{string.position} length #{string.length}"
# first line is integers, convention is that following lines are the same
TYPE_LENGTH.times { word = ((word << TYPE_BITS) + TYPE_INT) }
@stream.write_uint32( word )
#puts "String is #{string} at #{string.position.to_s(16)} length #{string.length}"
write_ref_for( string.get_layout ) #ref write_ref_for( string.get_layout ) #ref
@stream.write str @stream.write str
pad_after(str.length) pad_after(str.length + 4)
#puts "String (#{string.length.to_s(16)}) stream #{@stream.length.to_s(16)}" puts "String (#{string.length}) stream #{@stream.length}"
end end
def write_Symbol(sym) def write_Symbol(sym)
@ -202,12 +194,12 @@ module Register
@stream.write_uint8(0) @stream.write_uint8(0)
end end
after = stream_position after = stream_position
puts "padded #{length.to_s(16)} with #{pad.to_s(16)} stream #{before}/#{after}" puts "padded #{length} with #{pad} stream #{before}/#{after}"
end end
# return the stream length as hex # return the stream length as hex
def stream_position def stream_position
@stream.length.to_s(16) @stream.length
end end
end end