seperate position create and register
many test fixes
This commit is contained in:
parent
9c93b38b8f
commit
698c845297
@ -103,7 +103,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
previous = nil
|
previous = nil
|
||||||
sorted.each do |objekt|
|
sorted.each do |objekt|
|
||||||
next if objekt.is_a?( Parfait::BinaryCode) or objekt.is_a?( Risc::Label )
|
next unless Position.is_object(objekt)
|
||||||
before = at
|
before = at
|
||||||
unless( Position.set?(objekt))
|
unless( Position.set?(objekt))
|
||||||
raise objekt.class
|
raise objekt.class
|
||||||
|
@ -45,7 +45,7 @@ module Risc
|
|||||||
translator = Risc.machine.platform.translator
|
translator = Risc.machine.platform.translator
|
||||||
cpu_jump = translator.translate(jump)
|
cpu_jump = translator.translate(jump)
|
||||||
pos = at + code.padded_length - cpu_jump.byte_length
|
pos = at + code.padded_length - cpu_jump.byte_length
|
||||||
Position.new(cpu_jump , pos)
|
Position.create(cpu_jump).set(pos)
|
||||||
cpu_jump.assemble(JumpWriter.new(code))
|
cpu_jump.assemble(JumpWriter.new(code))
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ module Risc
|
|||||||
first = nil
|
first = nil
|
||||||
while code
|
while code
|
||||||
raise "Not Binary Code #{code.class}" unless code.is_a?(Parfait::BinaryCode)
|
raise "Not Binary Code #{code.class}" unless code.is_a?(Parfait::BinaryCode)
|
||||||
position = Position.new(code , -1)
|
position = Position.get_or_create(code)
|
||||||
first = position unless first
|
first = position unless first
|
||||||
position.position_listener(CodeListener.new)
|
position.position_listener(CodeListener.new)
|
||||||
code = code.next
|
code = code.next
|
||||||
|
@ -69,7 +69,7 @@ module Risc
|
|||||||
# - trigger change for this (fake) to set position of inserted
|
# - trigger change for this (fake) to set position of inserted
|
||||||
def position_inserted(position)
|
def position_inserted(position)
|
||||||
inserted = position.object.next
|
inserted = position.object.next
|
||||||
new_pos = Position.new(inserted , -1)
|
new_pos = Position.get_or_create(inserted)
|
||||||
new_pos.position_listener(InstructionListener.new(@binary))
|
new_pos.position_listener(InstructionListener.new(@binary))
|
||||||
|
|
||||||
position.trigger_changing(position.at)
|
position.trigger_changing(position.at)
|
||||||
@ -89,7 +89,7 @@ module Risc
|
|||||||
raise "Must init with instruction, not nil" unless instruction
|
raise "Must init with instruction, not nil" unless instruction
|
||||||
first = nil
|
first = nil
|
||||||
while(instruction)
|
while(instruction)
|
||||||
position = Position.new(instruction , -1)
|
position = Position.get_or_create(instruction)
|
||||||
first = position unless first
|
first = position unless first
|
||||||
il = InstructionListener.new( code )
|
il = InstructionListener.new( code )
|
||||||
position.position_listener(il)
|
position.position_listener(il)
|
||||||
|
@ -20,18 +20,19 @@ module Risc
|
|||||||
include Util::Logging
|
include Util::Logging
|
||||||
log_level :info
|
log_level :info
|
||||||
|
|
||||||
|
INVALID = -1
|
||||||
|
|
||||||
include Util::Eventable
|
include Util::Eventable
|
||||||
|
|
||||||
attr_reader :at , :object
|
attr_reader :at , :object
|
||||||
|
|
||||||
# initialize with a given object, first parameter
|
# initialize with a given object, first parameter
|
||||||
# The object ill be the key in global position map
|
# The object will be the key in global position map
|
||||||
# Give an integer as the actual position, where -1
|
#
|
||||||
# which means no legal position known
|
# The actual position starts as -1 (invalid)
|
||||||
def initialize(object , pos )
|
def initialize(object)
|
||||||
@at = pos
|
@at = INVALID
|
||||||
@object = object
|
@object = object
|
||||||
Position.set_to(self , pos)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# utility to register events of type :position_changed
|
# utility to register events of type :position_changed
|
||||||
@ -62,7 +63,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
@at != -1
|
@at != INVALID
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(int)
|
def set(int)
|
||||||
@ -71,7 +72,7 @@ module Risc
|
|||||||
Position.set_to(self , int)
|
Position.set_to(self , int)
|
||||||
@at = int
|
@at = int
|
||||||
trigger_changed
|
trigger_changed
|
||||||
int
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
# helper to fire the event that the position is about to change
|
# helper to fire the event that the position is about to change
|
||||||
@ -164,6 +165,23 @@ module Risc
|
|||||||
pos
|
pos
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# creating means instantiating and caching
|
||||||
|
def self.create(object)
|
||||||
|
pos = Position.new(object)
|
||||||
|
self.positions[object] = pos
|
||||||
|
log.debug "Initialize for #{object.class} #{object.object_id.to_s(16)}"
|
||||||
|
pos
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.get_or_create(object)
|
||||||
|
if self.positions.has_key?(object)
|
||||||
|
pos = self.get(object)
|
||||||
|
else
|
||||||
|
pos = self.create(object)
|
||||||
|
end
|
||||||
|
return pos
|
||||||
|
end
|
||||||
|
|
||||||
# populate the position caches (forward and revese) with the given position
|
# populate the position caches (forward and revese) with the given position
|
||||||
# forward caches object -> position
|
# forward caches object -> position
|
||||||
# reverse caches position.at > position
|
# reverse caches position.at > position
|
||||||
@ -178,13 +196,26 @@ module Risc
|
|||||||
end
|
end
|
||||||
self.positions[position.object] = position
|
self.positions[position.object] = position
|
||||||
@reverse_cache[to] = position unless position.object.is_a?(Label)
|
@reverse_cache[to] = position unless position.object.is_a?(Label)
|
||||||
if to == -1
|
if to == INVALID
|
||||||
log.debug "Initialize for #{position.object.class} #{position.object.object_id.to_s(16)}"
|
raise "Old style, change code"
|
||||||
else
|
else
|
||||||
log.debug "Set #{position} to 0x#{to.to_s(16)} for #{position.object.class} #{position.object.object_id.to_s(16)}"
|
log.debug "Set #{position} to 0x#{to.to_s(16)} for #{position.object.class} #{position.object.object_id.to_s(16)}"
|
||||||
end
|
end
|
||||||
position
|
position
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.is_object(object)
|
||||||
|
case object
|
||||||
|
when Risc::Label , Parfait::BinaryCode
|
||||||
|
return false
|
||||||
|
when Parfait::Object , Symbol
|
||||||
|
return true
|
||||||
|
when Arm::Instruction , Risc::Branch
|
||||||
|
return false
|
||||||
|
else
|
||||||
|
raise "Class #{object.class}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
require_relative "position_listener"
|
require_relative "position_listener"
|
||||||
|
@ -50,22 +50,10 @@ module Risc
|
|||||||
# Write all the objects in the order that they have been positioed
|
# Write all the objects in the order that they have been positioed
|
||||||
def write_objects
|
def write_objects
|
||||||
sorted_objects.each do |objekt|
|
sorted_objects.each do |objekt|
|
||||||
next unless is_object(objekt)
|
next unless Position.is_object(objekt)
|
||||||
write_any( objekt )
|
write_any( objekt )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def is_object(object)
|
|
||||||
case object
|
|
||||||
when Risc::Label , Parfait::BinaryCode
|
|
||||||
return false
|
|
||||||
when Parfait::Object , Symbol
|
|
||||||
return true
|
|
||||||
when Arm::Instruction
|
|
||||||
return false
|
|
||||||
else
|
|
||||||
raise "Class #{object.class}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# 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
|
||||||
|
@ -18,7 +18,7 @@ module Arm
|
|||||||
@machine = Arm::ArmMachine
|
@machine = Arm::ArmMachine
|
||||||
@binary = FakeBin.new
|
@binary = FakeBin.new
|
||||||
Risc::Position.clear_positions
|
Risc::Position.clear_positions
|
||||||
Risc::Position.new(@binary , 0)
|
Risc::Position.create(@binary).set(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
# code is what the generator spits out, at least one instruction worth (.first)
|
# code is what the generator spits out, at least one instruction worth (.first)
|
||||||
|
@ -18,9 +18,9 @@ module Arm
|
|||||||
def test_method_call
|
def test_method_call
|
||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
bin = Parfait::BinaryCode.new(1)
|
bin = Parfait::BinaryCode.new(1)
|
||||||
Risc::Position.new(bin , 0x20)
|
Risc::Position.new(bin).set(0x20)
|
||||||
code = @machine.call( bin ,{} )#this jumps to the next instruction
|
code = @machine.call( bin ,{} )#this jumps to the next instruction
|
||||||
Risc::Position.new(code, 0)
|
Risc::Position.new(code).set(0)
|
||||||
assert_code code , :call, [0x08,0x0,0x0,0xeb]
|
assert_code code , :call, [0x08,0x0,0x0,0xeb]
|
||||||
end
|
end
|
||||||
def test_swi
|
def test_swi
|
||||||
|
@ -82,7 +82,7 @@ module Arm
|
|||||||
end
|
end
|
||||||
def test_too_big_add
|
def test_too_big_add
|
||||||
code = @machine.add :r1 , :r1, 0x222
|
code = @machine.add :r1 , :r1, 0x222
|
||||||
Risc::Position.new(code,0)
|
Risc::Position.create(code).set(0)
|
||||||
# add 0x02 (first instruction) and then 0x220 shifted
|
# add 0x02 (first instruction) and then 0x220 shifted
|
||||||
assert_code code , :add , [0x02,0x1c,0x91,0xe2] #e2 91 1e 02
|
assert_code code , :add , [0x02,0x1c,0x91,0xe2] #e2 91 1e 02
|
||||||
# added extra instruction to add "extra"
|
# added extra instruction to add "extra"
|
||||||
@ -91,14 +91,14 @@ module Arm
|
|||||||
|
|
||||||
def label( pos = 0x22 + 8)
|
def label( pos = 0x22 + 8)
|
||||||
label = Risc::Label.new("some" , "Label" , FakeAddress.new(pos))
|
label = Risc::Label.new("some" , "Label" , FakeAddress.new(pos))
|
||||||
Risc::Position.new(label , pos)
|
Risc::Position.create(label).set(pos)
|
||||||
#Risc::Position.set(l , pos , @binary)
|
#Risc::Position.set(l , pos , @binary)
|
||||||
label
|
label
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_move_object
|
def test_move_object
|
||||||
code = @machine.add( :r1 , label)
|
code = @machine.add( :r1 , label)
|
||||||
Risc::Position.new(code,0)
|
Risc::Position.create(code).set(0)
|
||||||
assert_code code , :add , [0x22,0x10,0x9f,0xe2] #e2 9f 10 22
|
assert_code code , :add , [0x22,0x10,0x9f,0xe2] #e2 9f 10 22
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ module Arm
|
|||||||
end
|
end
|
||||||
def test_mov_big
|
def test_mov_big
|
||||||
code = @machine.mov :r0, 0x222 # is not 8 bit and can't be rotated by the arm system in one instruction
|
code = @machine.mov :r0, 0x222 # is not 8 bit and can't be rotated by the arm system in one instruction
|
||||||
Risc::Position.new(code,0)
|
Risc::Position.new(code).set(0)
|
||||||
# mov 512(0x200) = e3 a0 0c 02 add 34(0x22) = e2 90 00 22
|
# mov 512(0x200) = e3 a0 0c 02 add 34(0x22) = e2 90 00 22
|
||||||
assert_code code , :mov , [ 0x02,0x0c,0xb0,0xe3]
|
assert_code code , :mov , [ 0x02,0x0c,0xb0,0xe3]
|
||||||
assert_code code.next , :add , [ 0x22,0x00,0x90,0xe2]
|
assert_code code.next , :add , [ 0x22,0x00,0x90,0xe2]
|
||||||
|
@ -5,13 +5,13 @@ module Risc
|
|||||||
def setup
|
def setup
|
||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
@binary = Parfait::BinaryCode.new(1)
|
@binary = Parfait::BinaryCode.new(1)
|
||||||
@bin_pos = Position.new(@binary,0)
|
@bin_pos = Position.new(@binary).set(0)
|
||||||
@instruction = DummyInstruction.new(DummyInstruction.new)
|
@instruction = DummyInstruction.new(DummyInstruction.new)
|
||||||
@position = InstructionListener.init(@instruction , @binary)
|
@position = InstructionListener.init(@instruction , @binary)
|
||||||
end
|
end
|
||||||
def test_label_address
|
def test_label_address
|
||||||
label = Label.new("hi" ,"ho" , FakeAddress.new(0))
|
label = Label.new("hi" ,"ho" , FakeAddress.new(0))
|
||||||
label_pos = Position.new( label , -1 )
|
label_pos = Position.new( label )
|
||||||
label_pos.position_listener(InstructionListener.new(@binary))
|
label_pos.position_listener(InstructionListener.new(@binary))
|
||||||
label_pos.set(8)
|
label_pos.set(8)
|
||||||
assert_equal 8 , label_pos.object.address.value
|
assert_equal 8 , label_pos.object.address.value
|
||||||
@ -52,8 +52,8 @@ module Risc
|
|||||||
def test_label_at_branch
|
def test_label_at_branch
|
||||||
label = Label.new("Hi","Ho" , FakeAddress.new(5) , @instruction)
|
label = Label.new("Hi","Ho" , FakeAddress.new(5) , @instruction)
|
||||||
branch = Branch.new("b" , label)
|
branch = Branch.new("b" , label)
|
||||||
Position.new(label , 8 )
|
Position.new(label ).set(8)
|
||||||
Position.new(branch , 8 )
|
Position.new(branch).set(8)
|
||||||
at_8 = Position.at(8)
|
at_8 = Position.at(8)
|
||||||
assert_equal Position , at_8.class
|
assert_equal Position , at_8.class
|
||||||
assert_equal Branch , at_8.object.class
|
assert_equal Branch , at_8.object.class
|
||||||
|
@ -6,9 +6,9 @@ module Risc
|
|||||||
def setup
|
def setup
|
||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
@label = Label.new("Hi","Ho" , FakeAddress.new(5))
|
@label = Label.new("Hi","Ho" , FakeAddress.new(5))
|
||||||
@label_pos = Position.new(@label , -1)
|
@label_pos = Position.new(@label ).set(4)
|
||||||
@code = Parfait::BinaryCode.new(1)
|
@code = Parfait::BinaryCode.new(1)
|
||||||
@code_pos = Position.new(@code , -1)
|
@code_pos = Position.new(@code)
|
||||||
@code_pos.position_listener( LabelListener.new(@label))
|
@code_pos.position_listener( LabelListener.new(@label))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ module Risc
|
|||||||
class TestPosition < MiniTest::Test
|
class TestPosition < MiniTest::Test
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@pos = Position.new(self , -1)
|
@pos = Position.new(self )
|
||||||
end
|
end
|
||||||
def test_new
|
def test_new
|
||||||
assert @pos
|
assert @pos
|
||||||
@ -15,7 +15,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_next_slot
|
def test_next_slot
|
||||||
mov = Arm::ArmMachine.mov(:r1 , :r1)
|
mov = Arm::ArmMachine.mov(:r1 , :r1)
|
||||||
position = Position.new(mov , 0)
|
position = Position.new(mov ).set(0)
|
||||||
assert_equal 4, position.next_slot
|
assert_equal 4, position.next_slot
|
||||||
end
|
end
|
||||||
def test_has_get_code
|
def test_has_get_code
|
||||||
@ -38,7 +38,7 @@ module Risc
|
|||||||
assert_equal 1 , @pos.position_listeners.length
|
assert_equal 1 , @pos.position_listeners.length
|
||||||
end
|
end
|
||||||
def test_set
|
def test_set
|
||||||
assert_equal 0 , @pos.set(0)
|
assert_equal 0 , @pos.set(0).at
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,7 @@ module Risc
|
|||||||
def setup
|
def setup
|
||||||
Position.clear_positions
|
Position.clear_positions
|
||||||
@instruction = DummyInstruction.new
|
@instruction = DummyInstruction.new
|
||||||
@position = Position.new(@instruction , 0)
|
@position = Position.new(@instruction).set(0)
|
||||||
@listener = PositionListener.new( @instruction )
|
@listener = PositionListener.new( @instruction )
|
||||||
end
|
end
|
||||||
def test_has_register
|
def test_has_register
|
||||||
@ -24,7 +24,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_no_fire_after_unregister
|
def test_no_fire_after_unregister
|
||||||
@object = @instruction
|
@object = @instruction
|
||||||
Position.new(self, 10)
|
Position.new(self).set(10)
|
||||||
assert @position.register_event(:position_changed , self)#can't use helper
|
assert @position.register_event(:position_changed , self)#can't use helper
|
||||||
assert @position.remove_position_listener(self)
|
assert @position.remove_position_listener(self)
|
||||||
@position.trigger(:position_changed , @position)
|
@position.trigger(:position_changed , @position)
|
||||||
@ -38,7 +38,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_position_set_triggers
|
def test_position_set_triggers
|
||||||
@object = @instruction
|
@object = @instruction
|
||||||
Position.new(self, 0)
|
Position.new(self).set(0)
|
||||||
@position.register_event(:position_changed , self)#can't use helper
|
@position.register_event(:position_changed , self)#can't use helper
|
||||||
@position.set(10)
|
@position.set(10)
|
||||||
assert_equal @position , @trigger
|
assert_equal @position , @trigger
|
||||||
|
@ -4,7 +4,7 @@ module Risc
|
|||||||
class TestPositionMath < MiniTest::Test
|
class TestPositionMath < MiniTest::Test
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@pos = Position.new(self , 5)
|
@pos = Position.new(self).set(5)
|
||||||
end
|
end
|
||||||
def test_add
|
def test_add
|
||||||
res = @pos + 5
|
res = @pos + 5
|
||||||
@ -15,24 +15,24 @@ module Risc
|
|||||||
assert_equal 2 , res
|
assert_equal 2 , res
|
||||||
end
|
end
|
||||||
def test_sub_pos
|
def test_sub_pos
|
||||||
res = @pos - Position.new(@pos,4)
|
res = @pos - Position.new(@pos).set(4)
|
||||||
assert_equal 1 , res
|
assert_equal 1 , res
|
||||||
end
|
end
|
||||||
def test_lg
|
def test_lg
|
||||||
assert @pos > Position.new(@pos,4)
|
assert @pos > Position.new(@pos).set(2)
|
||||||
end
|
end
|
||||||
def test_tos
|
def test_tos
|
||||||
assert_equal "0x5" , @pos.to_s
|
assert_equal "0x5" , @pos.to_s
|
||||||
end
|
end
|
||||||
def test_reset_ok
|
def test_reset_ok
|
||||||
pos = @pos.set(10)
|
pos = @pos.set(10)
|
||||||
assert_equal 10 , pos
|
assert_equal 10 , pos.at
|
||||||
end
|
end
|
||||||
def test_object_class_test
|
def test_object_class_test
|
||||||
assert_equal :object , @pos.object_class
|
assert_equal :object , @pos.object_class
|
||||||
end
|
end
|
||||||
def test_object_class_instr
|
def test_object_class_instr
|
||||||
assert_equal :instruction , Position.new(Label.new("hi","ho",FakeAddress.new(1)),4).object_class
|
assert_equal :instruction , Position.new(Label.new("hi","ho",FakeAddress.new(1))).set(4).object_class
|
||||||
end
|
end
|
||||||
def test_at
|
def test_at
|
||||||
pos = Position.at(5)
|
pos = Position.at(5)
|
||||||
|
@ -6,8 +6,8 @@ module Risc
|
|||||||
def setup
|
def setup
|
||||||
@object = Dummy.new
|
@object = Dummy.new
|
||||||
@dependent = Dummy.new
|
@dependent = Dummy.new
|
||||||
@pos = Position.new(@object,0)
|
@pos = Position.new(@object).set(0)
|
||||||
Position.new(@dependent,0)
|
Position.new(@dependent).set(0)
|
||||||
@listener = PositionListener.new(@dependent)
|
@listener = PositionListener.new(@dependent)
|
||||||
end
|
end
|
||||||
def test_register
|
def test_register
|
||||||
|
@ -20,6 +20,7 @@ module Risc
|
|||||||
@machine = Risc.machine.boot
|
@machine = Risc.machine.boot
|
||||||
@machine.translate(:arm)
|
@machine.translate(:arm)
|
||||||
@machine.position_all
|
@machine.position_all
|
||||||
|
@machine.create_binary
|
||||||
@text_writer = TextWriter.new(@machine)
|
@text_writer = TextWriter.new(@machine)
|
||||||
end
|
end
|
||||||
def test_write_all
|
def test_write_all
|
||||||
|
Loading…
Reference in New Issue
Block a user