Steamline objectPosition init

check for resetting on the same position with different class, which
should not happen
Ie it’s ok for an object to push another object up,
but not for an instruction to land on code
This commit is contained in:
Torsten Ruger 2018-05-23 21:34:49 +03:00
parent f5d1090c39
commit 8ca70a6835
6 changed files with 39 additions and 31 deletions

View File

@ -16,7 +16,7 @@ module Risc
module Position
include Util::Logging
log_level :info
log_level :debug
@positions = {}
@ -57,17 +57,21 @@ module Risc
def self.set( object , pos , extra = nil)
# resetting of position used to be error, but since relink and dynamic instruction size it is ok.
# in measures (of 32)
log.debug "Setting #{pos} for #{object.class}-#{object}" if pos < 3000
# in measures
log.debug "Setting #{pos.to_s(16)} for #{object.class}-#{object}"
old = Position.positions[object]
testing = self.at( pos )
if old != nil
raise "Mismatch was:#{old}#{old.class} , should #{testing}#{testing.class}" if testing and testing.class != old.class
old.reset_to(pos)
log.debug "Reset #{pos.to_s(16)} for #{old.class}"
return old
end
position = for_at( object , pos , extra)
raise "Mismatch was:#{position}#{position.class} , should #{testing}#{testing.class}" if testing and testing.class != old.class
self.positions[object] = position
position.init(pos)
log.debug "Set #{pos} for #{position.class}" if pos < 3000
log.debug "Set #{pos.to_s(16)} for #{position.class}"
position
end
@ -78,7 +82,7 @@ module Risc
when Arm::Instruction , Risc::Instruction
InstructionPosition.new(object,at , extra)
else
ObjectPosition.new(at,object)
ObjectPosition.new(object,at)
end
end
end

View File

@ -13,7 +13,7 @@ module Risc
attr_reader :code , :method
def initialize(code, pos , method)
super(pos,code)
super(code,pos)
@code = code
@method = method
raise "Method nil" unless method
@ -26,21 +26,22 @@ module Risc
next_meth = next_method
return unless next_meth
Position.set( next_meth.binary , next_pos , next_meth)
Position.set( next_meth.cpu_instructions, next_pos + 12 , next_meth.binary)
next_cpu_pos = next_pos + Parfait::BinaryCode.offset
Position.set( next_meth.cpu_instructions, next_cpu_pos , next_meth.binary)
end
end
def reset_to(pos)
super(pos)
#puts "Reset (#{changed}) #{instruction}"
Position.log.debug "Reset (#{pos.to_s(16)}) #{code}"
init(pos)
end
def next_method
next_m = @method.next_method
return next_m if next_m
#puts "Type now #{@method.for_type.name}"
Position.log.debug "Type now #{@method.for_type.name}"
type = next_type(@method.for_type)
if type
#puts "Position for #{type.name}"
Position.log.debug "Position for #{type.name}"
return type.methods
else
return nil

View File

@ -18,26 +18,26 @@ module Risc
attr_reader :instruction , :binary
def initialize(instruction, pos , binary)
raise "not set " unless binary
super(pos, instruction)
super(instruction,pos)
@instruction = instruction
@binary = binary
end
def init(at)
diff = at - Position.get(@binary).at
if( diff % 60 == 12*4)
if( diff % 60 == 13*4)
@binary.extend_one unless @binary.next
@binary = @binary.next
raise "end of line " unless @binary
at = Position.get(@binary).at + 3*4
at = Position.get(@binary).at + Parfait::BinaryCode.offset
end
return unless instruction.next
at += instruction.byte_length
Position.set(instruction.next, at , binary)
return unless @instruction.next
at += @instruction.byte_length
Position.set(@instruction.next, at , @binary)
end
def reset_to(pos)
super(pos)
#puts "Reset (#{changed}) #{instruction}"
Position.log.debug "Reset (#{pos}) #{instruction}"
init(pos)
end
end

View File

@ -3,7 +3,7 @@ module Risc
class ObjectPosition
attr_reader :at , :object
def initialize( at , object)
def initialize( object, at)
@at = at
@object = object
raise "not int #{self}-#{at}" unless @at.is_a?(Integer)

View File

@ -7,25 +7,25 @@ module Risc
def setup
Risc.machine.boot
@binary = Parfait::BinaryCode.new(1)
Position.set(@binary , 0,Parfait.object_space.get_main)
Position.set(@binary , 0 , Parfait.object_space.get_main)
@label = Label.new("hi","ho")
end
def test_set_instr
pos = Position.set( @label , 0 , @binary)
pos = Position.set( @label , 8 , @binary)
assert_equal InstructionPosition , pos.class
end
def test_ins_propagates
@label.set_next Arm::ArmMachine.b( @label)
Position.set( @label , 0 , @binary)
assert_equal 0 , Position.get(@label.next).at
Position.set( @label , 8 , @binary)
assert_equal 8 , Position.get(@label.next).at
end
def test_ins_propagates_again
second = Arm::ArmMachine.b( @label)
@label.set_next(second)
Position.set( @label , 0 , @binary)
Position.set( @label , 8 , @binary)
Position.set(second , 2 , @binary)
Position.set( @label , 0 , @binary)
assert_equal 0 , Position.get(@label.next).at
Position.set( @label , 8 , @binary)
assert_equal 8 , Position.get(@label.next).at
end
def test_label_at
branch = Branch.new("b" , @label)
@ -35,6 +35,9 @@ module Risc
assert_equal InstructionPosition , at_4.class
assert_equal Branch , at_4.instruction.class
end
def test_reset_false_type
assert_raises {Position.set(@label , 0 , @binary)}
end
end
end
end

View File

@ -6,22 +6,22 @@ module Risc
class TestPositionBasic < MiniTest::Test
def test_creation_ok
assert ObjectPosition.new(0,self)
assert ObjectPosition.new(self,0)
end
def test_creation_fail
assert_raises {Position.new("0")}
end
def test_add
res = ObjectPosition.new(0,self) + 5
res = ObjectPosition.new(self,0) + 5
assert_equal 5 , res
end
def test_sub
res = ObjectPosition.new(5,self) - 1
assert_equal 4 , res
res = ObjectPosition.new(self,0) - 1
assert_equal -1 , res
end
def test_sub_pos
res = ObjectPosition.new(5,self) - ObjectPosition.new(1,self)
assert_equal 4 , res
res = ObjectPosition.new(self,0) - ObjectPosition.new(self,0)
assert_equal 0 , res
end
def test_set
pos = Position.set(self , 5)