From 3cc9175efacbf4384a6b3136bfe966d6e486a8d8 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 14 Jun 2018 21:29:34 +0300 Subject: [PATCH] start BranchListener but on hold, since it needs positions before we have them Must create them during collection phase --- lib/risc/instructions/branch.rb | 4 ++++ lib/risc/position/branch_listener.rb | 22 +++++++++++++++++++ lib/risc/position/instruction_listener.rb | 9 ++++++-- test/risc/position/test_branch_listener.rb | 14 ++++++++++++ test/risc/position/test_code_listener.rb | 3 +-- test/risc/position/test_code_listener1.rb | 1 - .../position/test_instruction_listener.rb | 1 - 7 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 lib/risc/position/branch_listener.rb create mode 100644 test/risc/position/test_branch_listener.rb diff --git a/lib/risc/instructions/branch.rb b/lib/risc/instructions/branch.rb index c553fe06..72c9cd3e 100644 --- a/lib/risc/instructions/branch.rb +++ b/lib/risc/instructions/branch.rb @@ -23,6 +23,10 @@ module Risc end alias :inspect :to_s + # if branch is implemented it must return the label it branches to + def branch + label + end end # dynamic version of an Branch branch that jumps to the contents diff --git a/lib/risc/position/branch_listener.rb b/lib/risc/position/branch_listener.rb new file mode 100644 index 00000000..b807aacc --- /dev/null +++ b/lib/risc/position/branch_listener.rb @@ -0,0 +1,22 @@ +module Risc + + class BranchListener + + # initialize with the instruction listener + def initialize(listener) + @listener = listener + end + + + def position_changed(position) + end + + # don't react to insertion, as the CodeListener will take care + def position_inserted(position) + end + + # dont react, as we do the work in position_changed + def position_changing(position , to) + end + end +end diff --git a/lib/risc/position/instruction_listener.rb b/lib/risc/position/instruction_listener.rb index 5e0b5a5b..525255a4 100644 --- a/lib/risc/position/instruction_listener.rb +++ b/lib/risc/position/instruction_listener.rb @@ -13,7 +13,7 @@ module Risc # the same BinaryCode, or else move it and the code along # class InstructionListener - attr_reader :binary + attr_reader :binary , :index def initialize(binary) @binary = binary @@ -91,7 +91,12 @@ module Risc while(instruction) position = Position.new(instruction , -1) first = position unless first - position.position_listener(InstructionListener.new( code )) + il = InstructionListener.new( code ) + position.position_listener(il) + if instruction.respond_to?(:branch) +# label_pos = Position.get(instruction.branch) +# label_pos.position_listener( BranchListener.new(il)) + end instruction = instruction.next end first diff --git a/test/risc/position/test_branch_listener.rb b/test/risc/position/test_branch_listener.rb new file mode 100644 index 00000000..9bffdf92 --- /dev/null +++ b/test/risc/position/test_branch_listener.rb @@ -0,0 +1,14 @@ +require_relative "helper" + +module Risc + class TestBranchListener < MiniTest::Test + def setup + @machine = Risc.machine.boot + @machine.translate(:interpreter) + @machine.position_all + end + + def test_has_init + end + end +end diff --git a/test/risc/position/test_code_listener.rb b/test/risc/position/test_code_listener.rb index 8ccc2824..269b346c 100644 --- a/test/risc/position/test_code_listener.rb +++ b/test/risc/position/test_code_listener.rb @@ -1,8 +1,7 @@ require_relative "helper" module Risc - # tests that require a boot and test propagation - class TestcodeListener < MiniTest::Test + class TestCodeListener < MiniTest::Test def setup Risc.machine.boot @binary = Parfait::BinaryCode.new(1) diff --git a/test/risc/position/test_code_listener1.rb b/test/risc/position/test_code_listener1.rb index 19986a2b..d4cf9ca2 100644 --- a/test/risc/position/test_code_listener1.rb +++ b/test/risc/position/test_code_listener1.rb @@ -1,7 +1,6 @@ require_relative "helper" module Risc - # tests that require a boot and test propagation class TestCodeListenerFull < MiniTest::Test def setup @machine = Risc.machine.boot diff --git a/test/risc/position/test_instruction_listener.rb b/test/risc/position/test_instruction_listener.rb index 8be17ff9..6cf267e5 100644 --- a/test/risc/position/test_instruction_listener.rb +++ b/test/risc/position/test_instruction_listener.rb @@ -1,7 +1,6 @@ require_relative "helper" module Risc - # tests that require a boot and test propagation class TestInstructionListener < MiniTest::Test def setup Risc.machine.boot