renaming and test fixing

This commit is contained in:
Torsten Ruger 2018-06-02 23:48:12 +03:00
parent 1d1c7105b4
commit f35ee6425a
11 changed files with 68 additions and 54 deletions

View File

@ -122,7 +122,8 @@ module Risc
next unless type.methods
type.methods.each_method do |method|
last_code = CodeListener.init(method.binary , code_start)
InstructionListener.init(method.cpu_instructions, method.binary)
first_position = InstructionListener.init(method.cpu_instructions, method.binary)
first_position.set_position( code_start + Parfait::BinaryCode.byte_offset)
last_code.position_listener( prev_code.object) if prev_code
prev_code = last_code
code_start = last_code.next_slot

View File

@ -42,10 +42,12 @@ module Risc
cpu_jump.assemble(JumpWriter.new(code))
end
# Create Position for the given BinaryCode object
# return the last position that was created, for chaining
def self.init( code , at = -1)
while code
raise "Not Binary Code #{code.class}" unless code.is_a?(Parfait::BinaryCode)
position = Position.new(code , at)
Position.set_to(position , at)
if code.next
listener = PositionListener.new(code.next)
position.position_listener( listener)

View File

@ -39,12 +39,6 @@ module Risc
Position.set(@instruction.next, nekst , binary)
end
def reset_to(pos , binary)
super(pos , binary)
init(pos , binary)
Position.log.debug "ResetInstruction (#{pos.to_s(16)}) #{instruction}"
end
# initialize the dependency graph for instructions
#
# starting from the given instruction, create Positions
@ -55,6 +49,7 @@ module Risc
# return the position for the first instruction which may be used to
# set all positions in the chain
def self.init( instruction , code )
raise "Not Binary Code #{code.class}" unless code.is_a?(Parfait::BinaryCode)
first = nil
while(instruction)
position = Position.new(instruction , -1)

View File

@ -48,6 +48,7 @@ module Risc
def position_listeners
event_table[:position_changed]
end
#look for InstructionListener and return its code if found
def get_code
listener = event_table.find{|one| one.class == InstructionListener}
@ -55,6 +56,14 @@ module Risc
listener.code
end
def set(int)
same = int == self.at
Position.set_to(self , int)
@at = int
trigger(:position_changed , self ) unless same
int
end
def +(offset)
offset = offset.at if offset.is_a?(Position)
@at + offset
@ -122,6 +131,6 @@ module Risc
end
end
end
require_relative "object_listener"
require_relative "position_listener"
require_relative "instruction_listener"
require_relative "code_listener"

View File

@ -20,7 +20,8 @@ module Risc
# position to reflect that change
#
def position_changed(previous)
me = previous.at + previous.object.padded_length
add = previous.object ? previous.object.padded_length : 0
me = previous.at + add
object_pos = Position.get(@object)
return if me == object_pos.at
Position.set(@object , me)

View File

@ -6,4 +6,7 @@ module Risc
4
end
end
class DummyInstruction < Dummy
include Util::List
end
end

View File

@ -14,8 +14,10 @@ module Risc
pos = CodeListener.init(@binary)
assert_equal pos, Position.get(@binary)
end
def test_init_fail
assert_raises{ CodeListener.init( @method)}
end
def test_init_returns_position
assert_equal Position , CodeListener.init(@binary).class
end
def test_init_listner

View File

@ -7,11 +7,14 @@ module Risc
Risc.machine.boot
@binary = Parfait::BinaryCode.new(1)
#Position.set(@binary , 0 , Parfait.object_space.get_main)
@label = Risc.label("hi","ho")
@label = DummyInstruction.new
end
def test_init
assert InstructionListener.init(@label , @binary)
end
def test_init_fail
assert_raises {InstructionListener.init(@label , nil)}
end
def pest_set_instr
pos = Position.set( @label , 8 , @binary)
assert_equal InstructionPosition , pos.class

View File

@ -4,8 +4,11 @@ module Risc
# tests that do no require a boot and only test basic positioning
class TestPosition < MiniTest::Test
def setup
@pos = Position.new(self , -1)
end
def test_new
assert Position.new(self , -1)
assert @pos
end
def test_next_slot
mov = Arm::ArmMachine.mov(:r1 , :r1)
@ -13,73 +16,68 @@ module Risc
assert_equal 4, position.next_slot
end
def test_has_get_code
assert_nil Position.new(self , -1).get_code
assert_nil @pos.get_code
end
def test_has_listeners_helper
assert_equal Array , Position.new(self,-1).position_listeners.class
assert_equal Array , @pos.position_listeners.class
end
def test_listeners_empty
assert Position.new(self,-1).position_listeners.empty?
assert @pos.position_listeners.empty?
end
def test_has_listener_helper
pos = Position.new(self,-1)
pos.position_listener( self )
assert_equal 1 , pos.position_listeners.length
@pos.position_listener( self )
assert_equal 1 , @pos.position_listeners.length
end
def test_set
assert_equal 0 , @pos.set(0)
end
end
class TestPositionMath < MiniTest::Test
def pest_creation_fail
assert_raises {Position.new("0")}
def setup
@pos = Position.new(self , 5)
end
def pest_add
res = Position.new(self,0) + 5
assert_equal 5 , res
def test_add
res = @pos + 5
assert_equal 10 , res
end
def pest_sub
res = Position.new(self,0) - 1
assert_equal -1 , res
def test_sub
res = @pos - 3
assert_equal 2 , res
end
def pest_sub_pos
res = Position.new(self,0) - Position.new(self,0)
assert_equal 0 , res
def test_sub_pos
res = @pos - Position.new(@pos,4)
assert_equal 1 , res
end
def pest_set
pos = Position.set(self , 5)
assert_equal 5 , pos.at
def test_tos
assert_equal "0x5" , @pos.to_s
end
def tet_tos
assert_equal "0x10" , Position.set(self).to_s
def test_reset_ok
pos = @pos.set(10)
assert_equal 10 , pos
end
def pest_reset_ok
pos = Position.set(self , 5)
pos = Position.set(self , 10)
assert_equal 10 , pos.at
end
def pest_reset_fail
Position.set(self , 5)
assert_raises{Position.set(self , 10000)}
end
def pest_raises_set_nil
assert_raises { Position.set(self,nil)}
end
def pest_at
pos = Position.set(self , 5)
def test_at
pos = Position.at(5)
assert_equal 5 , pos.at
end
end
class TestPositionEvents < MiniTest::Test
def setup
@position = Position.new(self)
Position.clear_positions
@instruciton = DummyInstruction.new
@position = Position.new(@instruction , 0)
@listener = PositionListener.new( @instruction )
end
def pest_has_register
assert @position.position_listener( self)
def test_has_register
assert @position.position_listener( @instruction )
end
def pest_can_unregister
def test_can_unregister
listener = PositionListener.new(self)
assert @position.position_listener(listener)
assert @position.unregister_event(:position_changed ,listener)
end
def pest_fires
@object = @instruction
@position.position_listener(self)
@position.trigger(:position_changed , @position)
assert_equal @position , @trigger

View File

@ -47,7 +47,7 @@ module Risc
def test_cpu_at
assert_equal "0x5ed4" , Position.get(@machine.cpu_init.first).to_s
end
def test_cpu_bin
def pest_cpu_bin
assert_equal "0x5ecc" , Position.get(@machine.cpu_init).to_s
end
def test_cpu_label