introduce the LabeListener to move instructions along when first code position changes
This commit is contained in:
@ -14,7 +14,7 @@ module Risc
|
||||
# fire events for changed pc and register contents
|
||||
include Util::Eventable
|
||||
include Util::Logging
|
||||
log_level :debug
|
||||
log_level :info
|
||||
|
||||
attr_reader :instruction , :clock , :pc # current instruction and pc
|
||||
attr_reader :registers # the registers, 16 (a hash, sym -> contents)
|
||||
|
@ -120,12 +120,13 @@ module Risc
|
||||
Parfait.object_space.types.values.each do |type|
|
||||
next unless type.methods
|
||||
type.methods.each_method do |method|
|
||||
Position.log.debug "Position starts for method #{method.name}"
|
||||
last_code = CodeListener.init(method.binary)
|
||||
last_code.set(code_start)
|
||||
first_position = InstructionListener.init(method.cpu_instructions, method.binary)
|
||||
first_position.set( code_start + Parfait::BinaryCode.byte_offset)
|
||||
code_start = last_code.next_slot
|
||||
#next unless method.name == :main or method.name == :__init__
|
||||
Position.log.debug "Method start #{code_start.to_s(16)} #{method.name}"
|
||||
code_pos = CodeListener.init(method.binary)
|
||||
InstructionListener.init(method.cpu_instructions, method.binary)
|
||||
code_pos.position_listener( LabelListener.new(method.cpu_instructions))
|
||||
code_pos.set(code_start)
|
||||
code_start = Position.get(method.binary.last_code).next_slot
|
||||
end
|
||||
end
|
||||
#Position.set( first_method.cpu_instructions, code_start + Parfait::BinaryCode.byte_offset , first_method.binary)
|
||||
|
@ -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
|
||||
|
39
lib/risc/position/label_listener.rb
Normal file
39
lib/risc/position/label_listener.rb
Normal 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
|
@ -190,3 +190,4 @@ end
|
||||
require_relative "position_listener"
|
||||
require_relative "instruction_listener"
|
||||
require_relative "code_listener"
|
||||
require_relative "label_listener"
|
||||
|
Reference in New Issue
Block a user