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

View File

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

View File

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

View File

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

View File

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

View File

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