introduce the LabeListener to move instructions along when first code position changes

This commit is contained in:
Torsten Ruger
2018-06-09 22:13:43 +03:00
parent ec1d38f5a6
commit c7ad1d98ca
7 changed files with 94 additions and 12 deletions

View File

@ -25,6 +25,7 @@ module Risc
# Taking into account that BinaryCodes only take 13 instructions,
# meaning that chain may have to be extended
def position_changing(position , to)
Position.log.debug "Changing #{position} to #{to.to_s(16)}, bin #{Position.get(@binary)}"
update_index(to)
instruction = position.object
return unless instruction.next
@ -40,7 +41,7 @@ module Risc
def update_index(to)
index = (to - Position.get(@binary).at) / 4
raise "Invalid negative index #{@index} , #{Position.get(@binary)}" if index < Parfait::BinaryCode.type_length
raise "Invalid negative index #{index} , #{Position.get(@binary)}" if index < Parfait::BinaryCode.type_length
while(index >= (Parfait::BinaryCode.memory_size - 1) )
@binary = @binary.ensure_next
index = (to - Position.get(@binary).at) / 4

View File

@ -0,0 +1,39 @@
module Risc
# LabelListener is the one point that connects the BinaryCode
# and Instruction positions.
#
# LabelListener is instantiated with the first label of a Method
# and attached to the first BinaryCode.
#
# When the code moves, the label position is updated.
#
# The first code may get pushed by a previous method, and there is otherwise
# no way to react to this.
class LabelListener
attr_reader :label
# initialize with the first label of the method
def initialize(label)
@label = label
end
# The incoming position is the first BinaryCode of the method
# We simply position the Label (instance, see initialize) as the
# first entry in the BinaryCode, at BinaryCode.byte_offset
def position_changed(position)
label_pos = Position.get(@label)
label_pos.set(position + Parfait::BinaryCode.byte_offset)
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

View File

@ -190,3 +190,4 @@ end
require_relative "position_listener"
require_relative "instruction_listener"
require_relative "code_listener"
require_relative "label_listener"