From cf06642768ec52fdac47cabc52d367c73b0ea274 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 8 May 2018 19:59:43 +0300 Subject: [PATCH] start passing positions along inside the position code --- lib/risc/machine.rb | 2 +- lib/risc/position.rb | 30 ++++++++++++++---------------- test/arm/test_call.rb | 2 +- test/arm/test_logic.rb | 2 +- test/risc/test_position1.rb | 9 +-------- test/risc/test_position2.rb | 30 ++++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 test/risc/test_position2.rb diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index 022323e1..5e284a67 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -78,7 +78,7 @@ module Risc translate_arm unless @translated #need the initial jump at 0 and then functions Position.set(binary_init,0) - Position.set(cpu_init , 3 , binary_init) + Position.set(cpu_init , 12 , binary_init) @code_start = position_objects( binary_init.padded_length ) # and then everything code position_code diff --git a/lib/risc/position.rb b/lib/risc/position.rb index eba9dd13..0efc1a4a 100644 --- a/lib/risc/position.rb +++ b/lib/risc/position.rb @@ -36,7 +36,7 @@ module Risc "0x#{@at.to_s(16)}" end # just a callback after creation AND insertion - def init + def init(pos) end def reset_to(pos) return false if pos == at @@ -77,7 +77,7 @@ module Risc end position = for_at( object , pos , extra) self.positions[object] = position - position.init + position.init(pos) position end @@ -102,23 +102,17 @@ module Risc @binary = binary end - def set_position( position , binary = nil) - raise "invalid position #{position}" if position > 15 - binary = Risc::Position.get(self).binary unless binary - Risc::Position.set(self, position , binary) - position += byte_length / 4 #assumes 4 byte instructions, as does the whole setup - if self.next - if( 3 == position % 15) # 12 is the amount of instructions that fit into a BinaryCode - position = 3 - binary = binary.next - end - self.next.set_position( position , binary) - else - position + def init(at) + return unless instruction.next + at += instruction.byte_length + bin = binary + if( 12 == at % 60) + at = 12 + bin = binary.next end + Position.set(instruction.next, at , binary) end - def reset_to(pos) changed = super(pos) #puts "Reset (#{changed}) #{instruction}" @@ -135,5 +129,9 @@ module Risc @code = code @method = method end + def init(at) + return unless code.next + Position.set(code.next , at + code.padded_length, method) + end end end diff --git a/test/arm/test_call.rb b/test/arm/test_call.rb index 4378f470..989e46ac 100644 --- a/test/arm/test_call.rb +++ b/test/arm/test_call.rb @@ -20,7 +20,7 @@ module Arm bin = Parfait::BinaryCode.new(1) Risc::Position.set(bin , 0x20) code = @machine.call( bin ,{} )#this jumps to the next instruction - Risc::Position.set(code , 0) + Risc::Position.set(code , 0, 1) assert_code code , :call, [0x09,0x0,0x0,0xeb] end def test_swi diff --git a/test/arm/test_logic.rb b/test/arm/test_logic.rb index 0dbe1c4e..5316f8cf 100644 --- a/test/arm/test_logic.rb +++ b/test/arm/test_logic.rb @@ -82,7 +82,7 @@ module Arm end def test_too_big_add code = @machine.add :r1 , :r1, 0x222 - Risc::Position.set(0,0) + Risc::Position.set(code,0,1) # add 0x02 (first instruction) and then 0x220 shifted assert_code code , :add , [0x02,0x1c,0x91,0xe2] #e2 91 1e 02 # added extra instruction to add "extra" diff --git a/test/risc/test_position1.rb b/test/risc/test_position1.rb index 2cbda8a5..a11111af 100644 --- a/test/risc/test_position1.rb +++ b/test/risc/test_position1.rb @@ -1,12 +1,9 @@ require_relative "../helper" module Risc + # tests that do no require a boot and only test basic positioning class TestPositionBasic < MiniTest::Test - def setup - Risc.machine.boot - @binary = Parfait::BinaryCode.new(1) - end def test_creation_ok assert Position.new(0) end @@ -29,10 +26,6 @@ module Risc pos = Position.set(self , 5) assert_equal 5 , pos.at end - def test_set_instr - pos = Position.set( Risc::Label.new("hi","ho") , 0 , @binary) - assert_equal IPosition , pos.class - end def tet_tos assert_equal "0x10" , Position.set(self).to_s end diff --git a/test/risc/test_position2.rb b/test/risc/test_position2.rb new file mode 100644 index 00000000..ecdeb345 --- /dev/null +++ b/test/risc/test_position2.rb @@ -0,0 +1,30 @@ +require_relative "../helper" + +module Risc + # tests that require a boot and test propagation + class TestPositionBasic < MiniTest::Test + def setup + Risc.machine.boot + @binary = Parfait::BinaryCode.new(1) + @label = Label.new("hi","ho") + end + def test_set_instr + pos = Position.set( @label , 0 , @binary) + assert_equal IPosition , pos.class + end + def test_set_bin + pos = Position.set( @binary , 0 , Parfait.object_space.get_main) + assert_equal BPosition , 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 + end + def test_bin_propagates + @binary.extend_to(16) + Position.set( @binary , 0 , Parfait.object_space.get_main) + assert_equal @binary.padded_length , Position.get(@binary.next).at + end + end +end