From b24b65520da51216bdaaaf11dd0c60febe5588e7 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 26 Mar 2018 14:54:41 +0300 Subject: [PATCH] remove all that label stuff left over after rewrite from blocks to linked list --- lib/common/list.rb | 11 ++++++--- lib/risc/collector.rb | 2 +- lib/risc/instruction.rb | 26 ++++++++------------ lib/risc/instructions/branch.rb | 36 ---------------------------- lib/risc/instructions/label.rb | 42 +++------------------------------ test/risc/test_instructions.rb | 14 ----------- 6 files changed, 22 insertions(+), 109 deletions(-) diff --git a/lib/common/list.rb b/lib/common/list.rb index 4a6c88c5..0020bb66 100644 --- a/lib/common/list.rb +++ b/lib/common/list.rb @@ -50,11 +50,16 @@ module Common end alias :<< :append - def length( labels = [] ) - ret = 1 - ret += self.next.length( labels ) if self.next + def length + ret = 0 + self.each { ret += 1} ret end + def each(&block) + block.call(self) + @next.each(&block) if @next + end + end end diff --git a/lib/risc/collector.rb b/lib/risc/collector.rb index 62be5cb8..ddaafdef 100644 --- a/lib/risc/collector.rb +++ b/lib/risc/collector.rb @@ -14,7 +14,7 @@ module Risc return unless add_object( object , depth ) # probably should make labels or even instructions derive from Parfait::Object, but . . if object.is_a? Risc::Label - object.each_label { |l| self.add_object(l ,depth)} + object.each { |l| self.add_object(l ,depth) if l.is_a? Risc::Label} end return unless object.respond_to? :has_type? type = object.get_type diff --git a/lib/risc/instruction.rb b/lib/risc/instruction.rb index f8ba3a9c..21b20db7 100644 --- a/lib/risc/instruction.rb +++ b/lib/risc/instruction.rb @@ -30,10 +30,9 @@ module Risc end attr_reader :source - #TODO check if this is used. Maybe build an each for instructions - def to_arr labels = [] - ret = [self.class] - ret += self.next.to_arr(labels) if self.next + def to_arr + ret = [] + self.each {|ins| ret << ins} ret end @@ -46,36 +45,31 @@ module Risc translator.translate( self ) end - def assemble_all( io , labels = [] ) + def assemble_all( io ) self.assemble(io) - self.next.assemble_all(io, labels) if self.next + self.next.assemble_all(io) if self.next end def assemble io raise "Abstract called on #{self}" end - def total_byte_length( labels = []) - ret = self.byte_length - ret += self.next.total_byte_length(labels) if self.next - #puts "#{self.class.name} return #{ret}" + def total_byte_length + ret = 0 + self.each{|ins| ret += ins.byte_length} ret end - def set_position position , labels = [] + def set_position( position ) Positioned.set_position(self,position) position += byte_length if self.next - self.next.set_position(position , labels) + self.next.set_position( position ) else position end end - def each_label labels =[] , &block - self.next.each_label(labels , &block) if self.next - end - def class_source( derived) "#{self.class.name.split("::").last}: #{derived} #{source_mini}" end diff --git a/lib/risc/instructions/branch.rb b/lib/risc/instructions/branch.rb index c3d641ab..38e96f79 100644 --- a/lib/risc/instructions/branch.rb +++ b/lib/risc/instructions/branch.rb @@ -15,42 +15,6 @@ module Risc end alias :inspect :to_s - def length( labels = []) - ret = super(labels) - ret += self.label.length(labels) if self.label - ret - end - - def to_arr( labels = [] ) - ret = super(labels) - ret += self.label.to_arr(labels) if self.label - ret - end - - def total_byte_length labels = [] - ret = super(labels) - ret += self.label.total_byte_length(labels) if self.label - #puts "#{self.class.name} return #{ret}" - ret - end - - # labels have the same position as their next - def set_position( position , labels = []) - set_position self.label.set_position( position , labels ) if self.label - super(position,labels) - end - - def assemble_all( io , labels = []) - self.assemble(io) - self.label.assemble_all(io,labels) if self.label - self.next.assemble_all(io, labels) if self.next - end - - def each_label( labels =[] , &block) - super - self.label.each_label(labels , &block) if self.label - end - end # dynamic version of an Unconditional branch that jumps to the contents diff --git a/lib/risc/instructions/label.rb b/lib/risc/instructions/label.rb index 4c7bc295..5bf64f76 100644 --- a/lib/risc/instructions/label.rb +++ b/lib/risc/instructions/label.rb @@ -31,43 +31,13 @@ module Risc @name.split(".").length == 2 end - def to_arr labels = [] - return [] if labels.include?(self) - labels << self - super - end - - def length labels = [] - return 0 if labels.include?(self) - labels << self - ret = 1 - ret += self.next.length(labels) if self.next - ret - end - def assemble io end - def assemble_all io , labels = [] - return if labels.include?(self) or self.next.nil? - labels << self - self.next.assemble_all(io,labels) - end - - def total_byte_length labels = [] - return 0 if labels.include?(self) or self.next.nil? - labels << self - ret = self.next.total_byte_length(labels) - #puts "#{self.class.name} return #{ret}" - ret - end - # labels have the same position as their next - def set_position position , labels = [] - return position if labels.include?(self) - labels << self - super(position , labels) - self.next.set_position(position,labels) if self.next + def set_position( position ) + super(position) + self.next.set_position(position) if self.next end # shame we need this, just for logging @@ -75,12 +45,6 @@ module Risc 0 end - def each_label labels =[] , &block - return if labels.include?(self) - labels << self - block.yield(self) - super - end end def self.label( source , name , nekst = nil) diff --git a/test/risc/test_instructions.rb b/test/risc/test_instructions.rb index a9f16803..68b77725 100644 --- a/test/risc/test_instructions.rb +++ b/test/risc/test_instructions.rb @@ -61,20 +61,6 @@ module Risc assert_equal @label, @instruction.next assert_equal 2 , @instruction.length , @instruction.to_arr end - def test_each_label1 - @instruction.set_next @label - start = Label.new("test" , "test" , @instruction) - count = 0 - start.each_label { |l| count += 1 } - assert_equal 2 , count - end - def test_each_label2 - @instruction.set_next @branch - start = Label.new("test" , "test" , @instruction) - count = 0 - start.each_label { |l| count += 1 } - assert_equal 2 , count - end def test_label_is_method label = Label.new("test" , "Object.test") assert label.is_method