simplify writing binary

loop as should be, fixes the problem (one problem at least)
This commit is contained in:
Torsten Ruger 2018-05-13 18:01:45 +03:00
parent 866467ee5e
commit 4856b9891d
4 changed files with 18 additions and 11 deletions

View File

@ -117,6 +117,13 @@ module Parfait
"#{@for_type.object_class.name}:#{name}(#{arguments_type.inspect})" "#{@for_type.object_class.name}:#{name}(#{arguments_type.inspect})"
end end
def each_binary( &block )
bin = binary
while(bin) do
block.call( bin )
bin = bin.next
end
end
def each_method( &block ) def each_method( &block )
block.call( self ) block.call( self )
next_method.each_method( &block ) if next_method next_method.each_method( &block ) if next_method

View File

@ -53,6 +53,7 @@ module Risc
def write_objects def write_objects
sorted_objects.each do |objekt| sorted_objects.each do |objekt|
next if objekt.is_a? Risc::Label # ignore next if objekt.is_a? Risc::Label # ignore
next if objekt.is_a? Parfait::BinaryCode # ignore
write_any( objekt ) write_any( objekt )
end end
end end
@ -60,12 +61,11 @@ module Risc
# Write the BinaryCode objects of all methods to stream. # Write the BinaryCode objects of all methods to stream.
# Really like any other object, it's just about the ordering # Really like any other object, it's just about the ordering
def write_code def write_code
@machine.objects.each do |id, method| Parfait.object_space.each_type do |type|
next unless method.is_a? Parfait::TypedMethod type.each_method do |method|
binary = method.binary method.each_binary do |code|
while(binary) do write_any(code)
write_any( binary ) end
binary = binary.next
end end
end end
end end
@ -73,8 +73,8 @@ module Risc
# Write any object just logs a bit and passes to write_any_out # Write any object just logs a bit and passes to write_any_out
def write_any( obj ) def write_any( obj )
write_any_log( obj , "Write") write_any_log( obj , "Write")
if @stream.length != Position.get(obj).at if stream_position != Position.get(obj).at
#puts "Write #{obj.class}:0x#{obj.object_id.to_s(16)} at 0x#{stream_position.to_s(16)} not #{Position.get(obj)}" raise "Write #{obj.class}:0x#{obj.object_id.to_s(16)} at 0x#{stream_position.to_s(16)} not #{Position.get(obj)}"
end end
write_any_out(obj) write_any_out(obj)
write_any_log( obj , "Wrote") write_any_log( obj , "Wrote")

View File

@ -39,10 +39,10 @@ module Risc
assert_equal 0 , Position.get(@machine.cpu_init).at assert_equal 0 , Position.get(@machine.cpu_init).at
end end
def test_cpu_at def test_cpu_at
assert_equal "0x5ad0" , Position.get(@machine.cpu_init.first).to_s assert_equal "0x5ad8" , Position.get(@machine.cpu_init.first).to_s
end end
def test_cpu_bin def test_cpu_bin
assert_equal "0x5ac4" , Position.get(Position.get(@machine.cpu_init.first).binary).to_s assert_equal "0x5acc" , Position.get(Position.get(@machine.cpu_init.first).binary).to_s
end end
def test_cpu_label def test_cpu_label
assert_equal Position::InstructionPosition , Position.get(@machine.cpu_init.first).class assert_equal Position::InstructionPosition , Position.get(@machine.cpu_init.first).class

View File

@ -30,6 +30,7 @@ module Risc
def test_sorted_positions1 def test_sorted_positions1
sorted_objects = @text_writer.sorted_objects sorted_objects = @text_writer.sorted_objects
sorted_objects.each_slice(2) do |l,r| sorted_objects.each_slice(2) do |l,r|
next unless r
assert Position.get(l).at < Position.get(r).at , "#{Position.get(l)} < #{Position.get(r)} , #{l.class}, #{r.class}" assert Position.get(l).at < Position.get(r).at , "#{Position.get(l)} < #{Position.get(r)} , #{l.class}, #{r.class}"
end end
end end
@ -37,7 +38,6 @@ module Risc
sorted_objects = @text_writer.sorted_objects sorted_objects = @text_writer.sorted_objects
sorted_objects.shift sorted_objects.shift
sorted_objects.each_slice(2) do |l,r| sorted_objects.each_slice(2) do |l,r|
next unless l
next unless r next unless r
assert Position.get(l).at < Position.get(r).at , "#{Position.get(l)} < #{Position.get(r)} , #{l.class}, #{r.class}" assert Position.get(l).at < Position.get(r).at , "#{Position.get(l)} < #{Position.get(r)} , #{l.class}, #{r.class}"
end end