diff --git a/lib/elf/object_writer.rb b/lib/elf/object_writer.rb index 806a53ad..60ca5fbb 100644 --- a/lib/elf/object_writer.rb +++ b/lib/elf/object_writer.rb @@ -29,7 +29,7 @@ module Elf next unless label.is_a?(Risc::Label) add_symbol "#{type.name}@#{meth.name}:Label=#{label.name}" , Risc::Position.get(label).at end - meth.binary.each do |code| + meth.binary.each_block do |code| label = "BinaryCode@#{Risc::Position.get(code).method.name}" add_symbol label , Risc::Position.get(code).at end diff --git a/lib/parfait/binary_code.rb b/lib/parfait/binary_code.rb index c5918f59..3c720b83 100644 --- a/lib/parfait/binary_code.rb +++ b/lib/parfait/binary_code.rb @@ -12,6 +12,17 @@ module Parfait 2 * 4 # size of type (2, type+next) * word_size (4) end + #16 - 2 -1 , two instance variables and one for the jump + def self.data_length + 13 + end + def data_length + self.class.data_length + end + def byte_length + 4*data_length + end + def initialize(total_size) super() extend_to(total_size ) @@ -32,7 +43,7 @@ module Parfait end end - def each( &block ) + def each_block( &block ) block.call( self ) @next.each( &block ) if @next end @@ -41,20 +52,16 @@ module Parfait "BinaryCode #{Risc::Position.set?(self) ? Risc::Position.get(self): self.object_id.to_s(16)}" end - def each_word + def each_word( all = true) index = 0 - while( index < data_length) + length = data_length + length += 1 if all + while( index < length) yield get_word(index) index += 1 end end - #16 - 2 -1 , two instance variables and one for the jump - def data_length - 13 - end - def byte_length - 4*data_length - end + def set_word(index , word) raise "invalid index #{index}" if index < 0 if index > data_length @@ -64,6 +71,9 @@ module Parfait end set_internal_word(index + 2 , word) end + def set_last(word) + set_word( data_length , word) + end def get_word(index) raise "invalid index #{index}" if index < 0 if index > data_length + 1 diff --git a/lib/risc/binary_writer.rb b/lib/risc/binary_writer.rb index ce58cede..a40adcb1 100644 --- a/lib/risc/binary_writer.rb +++ b/lib/risc/binary_writer.rb @@ -27,13 +27,13 @@ module Risc end #even less glue to get that last jump in there. - # So instructions don't run into the BinaryCode object header + # So instructions don't run into the BinaryCode object header class JumpWriter def initialize( code ) @code = code end def write_unsigned_int_32( bin ) - @code.set_word( 14 , bin ) + @code.set_last( bin ) end end end diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index df98813f..b8858132 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -108,7 +108,7 @@ module Risc Position.set( first_method.binary , at , first_method) Position.set( first_method.cpu_instructions, at + Parfait::BinaryCode.offset , first_method.binary) log.debug "Method #{first_method.name}:#{before.to_s(16)} len: #{(at - before).to_s(16)}" - log.debug "Instructions #{first_method.cpu_instructions.object_id.to_s(16)}:#{(before+12).to_s(16)}" + log.debug "Instructions #{first_method.cpu_instructions.object_id.to_s(16)}:#{(before+Parfait::BinaryCode.offset).to_s(16)}" at end diff --git a/lib/risc/position/code_position.rb b/lib/risc/position/code_position.rb index f108b44a..d20d01a4 100644 --- a/lib/risc/position/code_position.rb +++ b/lib/risc/position/code_position.rb @@ -33,8 +33,8 @@ module Risc end end def reset_to(pos , ignored) - super(pos, ignored) init(pos , ignored) + super(pos, ignored) Position.log.debug "ResetCode (#{pos.to_s(16)}) #{code}" end # insert a jump to the next instruction, at the last instruction diff --git a/lib/risc/position/instruction_position.rb b/lib/risc/position/instruction_position.rb index 6e46eebe..4c52476f 100644 --- a/lib/risc/position/instruction_position.rb +++ b/lib/risc/position/instruction_position.rb @@ -24,7 +24,7 @@ module Risc end def init(at, binary) return if at == 0 and binary.nil? - raise "faux pas" if Position.get(instruction).at < Position.get(binary).at + raise "faux pas" if at < Position.get(binary).at return unless @instruction.next @binary = binary nekst = at + @instruction.byte_length @@ -43,8 +43,8 @@ module Risc end def reset_to(pos , binary) - super(pos , binary) init(pos , binary) + super(pos , binary) Position.log.debug "ResetInstruction (#{pos.to_s(16)}) #{instruction}" end end diff --git a/lib/risc/text_writer.rb b/lib/risc/text_writer.rb index 1467e8d2..034ebab4 100644 --- a/lib/risc/text_writer.rb +++ b/lib/risc/text_writer.rb @@ -162,7 +162,6 @@ module Risc code.each_word do |word| @stream.write_unsigned_int_32( word || 0 ) end - write_ref_for( code.get_type ) log.debug "Code16 witten stream 0x#{@stream.length.to_s(16)}" end @@ -180,7 +179,7 @@ module Risc write_ref_for( string.get_type ) #ref @stream.write_signed_int_32( str.length ) #int @stream.write str - pad_after(str.length + 8 ) # type , length *4 == 12 + pad_after(str.length + 8 ) # type , length log.debug "String (0x#{string.length.to_s(16)}) stream 0x#{@stream.length.to_s(16)}" end diff --git a/test/parfait/test_binary_code.rb b/test/parfait/test_binary_code.rb index 8f41aab6..d4c62776 100644 --- a/test/parfait/test_binary_code.rb +++ b/test/parfait/test_binary_code.rb @@ -80,17 +80,23 @@ module Parfait assert @code.next.next assert_nil @code.next.next.next end - def test_each + def test_each_word + len = 0 + @code.each_word(false){ len += 1} + assert_equal 13 , len + end + def test_each_word_all len = 0 @code.each_word{ len += 1} - assert_equal 13 , len + assert_equal 14 , len end def test_each_set (0..12).each{|i| @code.set_word(i,i)} all = [] - @code.each_word{ |w| all << w} + @code.each_word(false){ |w| all << w} assert_equal 0 , all.first assert_equal 12 , all.last + assert_nil @code.next end def test_set_word assert_equal 1 , @code.set_word(1 , 1) @@ -103,9 +109,21 @@ module Parfait @code.set_word(1 , 1) assert_equal 1, @code.get_internal_word(@code.data_start + 1) end + def test_set_13 + @code.set_word(13 , 1) + assert_nil @code.next + end + def test_set_last_no_extend + @code.set_last(1) + assert_nil @code.next + end + def test_set_last_and_get + @code.set_last(1) + assert_equal 1, @code.get_word(BinaryCode.data_length) + end def test_has_each sum = 0 - @code.each{ sum += 1} + @code.each_block{ sum += 1} assert_equal sum , 1 end end diff --git a/test/risc/test_machine.rb b/test/risc/test_machine.rb index b62d1a7c..78b2603c 100644 --- a/test/risc/test_machine.rb +++ b/test/risc/test_machine.rb @@ -37,5 +37,13 @@ module Risc def test_cpu_label assert_equal Position::InstructionPosition , Position.get(@machine.cpu_init.first).class end + def test_first_binary_jump + bin = Parfait.object_space.get_init.binary + assert 0 != bin.get_word(14) , "index 0 is 0 #{bin.inspect}" + end + def test_second_binary_first + bin = Parfait.object_space.get_init.binary.next + assert 0 != bin.get_word(0) , "index 0 is 0 #{bin.inspect}" + end end end diff --git a/test/risc/test_padding.rb b/test/risc/test_padding.rb index a133cf48..cc7c530e 100644 --- a/test/risc/test_padding.rb +++ b/test/risc/test_padding.rb @@ -40,10 +40,6 @@ module Risc word = Parfait::Word.new(12) assert_equal 32 , word.padded_length end - def test_pos_arm - mov = Arm::ArmMachine.mov :r1, 128 - Risc::Position.set(0,0) - end end end