jump was written off the end of binary code, fixed

This commit is contained in:
Torsten Ruger 2018-05-28 11:45:04 +03:00
parent 1c09d4202f
commit be1bc63ff2
10 changed files with 58 additions and 27 deletions

View File

@ -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

View File

@ -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

View File

@ -33,7 +33,7 @@ module Risc
@code = code
end
def write_unsigned_int_32( bin )
@code.set_word( 14 , bin )
@code.set_last( bin )
end
end
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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