diff --git a/lib/risc/instruction.rb b/lib/risc/instruction.rb index f8ed3ed3..364771fd 100644 --- a/lib/risc/instruction.rb +++ b/lib/risc/instruction.rb @@ -26,7 +26,7 @@ module Risc @source = source @next = nekst return unless source - raise "Source must be string or ast node, not #{source.class}" unless source.is_a?(String) or source.is_a?(Mom::Instruction) + raise "Source must be string or Instruction, not #{source.class}" unless source.is_a?(String) or source.is_a?(Mom::Instruction) end attr_reader :source @@ -72,6 +72,14 @@ module Risc self.next.each_label(labels , &block) if self.next end + def class_source( derived) + "#{self.class.name.split("::").last}: #{derived} #{source_mini}" + end + def source_mini + return "(no source)" unless source + return "(from: #{source[0..15]})" if source.is_a?(String) + "(from: #{source.class.name.split("::").last})" + end end end diff --git a/lib/risc/instructions/branch.rb b/lib/risc/instructions/branch.rb index 40ad0fcd..cb133a68 100644 --- a/lib/risc/instructions/branch.rb +++ b/lib/risc/instructions/branch.rb @@ -11,7 +11,7 @@ module Risc attr_reader :label def to_s - "#{self.class.name.split("::").last}: #{label ? label.name : ''}" + class_source "#{label ? label.name : '(no label)'}" end alias :inspect :to_s diff --git a/lib/risc/instructions/function_call.rb b/lib/risc/instructions/function_call.rb index abe6ec10..3a090c9c 100644 --- a/lib/risc/instructions/function_call.rb +++ b/lib/risc/instructions/function_call.rb @@ -12,7 +12,7 @@ module Risc attr_reader :method , :register def to_s - "FunctionCall: #{method.name}" + class_source method.name end end diff --git a/lib/risc/instructions/function_return.rb b/lib/risc/instructions/function_return.rb index b9894548..642a0651 100644 --- a/lib/risc/instructions/function_return.rb +++ b/lib/risc/instructions/function_return.rb @@ -11,7 +11,7 @@ module Risc attr_reader :register def to_s - "FunctionReturn: #{register} " + class_source "#{register} " end end diff --git a/lib/risc/instructions/getter.rb b/lib/risc/instructions/getter.rb index dfc84481..6d615b5c 100644 --- a/lib/risc/instructions/getter.rb +++ b/lib/risc/instructions/getter.rb @@ -30,7 +30,7 @@ module Risc attr_accessor :array , :index , :register def to_s - "#{self.class.name.split("::").last}: #{array}[#{index}] -> #{register}" + class_source "#{array}[#{index}] -> #{register}" end end diff --git a/lib/risc/instructions/label.rb b/lib/risc/instructions/label.rb index 0ec2f10c..c5482774 100644 --- a/lib/risc/instructions/label.rb +++ b/lib/risc/instructions/label.rb @@ -15,7 +15,7 @@ module Risc attr_reader :name def to_s - "Label: #{@name} (#{self.next.class.name.split("::").last})" + class_source "#{@name} (next: #{self.next.class.name.split("::").last})" end def sof_reference_name diff --git a/lib/risc/instructions/load_constant.rb b/lib/risc/instructions/load_constant.rb index 53a6c032..b6f7875e 100644 --- a/lib/risc/instructions/load_constant.rb +++ b/lib/risc/instructions/load_constant.rb @@ -14,7 +14,7 @@ module Risc attr_accessor :register , :constant def to_s - "LoadConstant: #{register} <- #{constant_str}" + class_source "#{register} <- #{constant_str}" end private diff --git a/lib/risc/instructions/operator_instruction.rb b/lib/risc/instructions/operator_instruction.rb index f7e75cd4..ef4d0420 100644 --- a/lib/risc/instructions/operator_instruction.rb +++ b/lib/risc/instructions/operator_instruction.rb @@ -10,7 +10,7 @@ module Risc attr_reader :operator, :left , :right def to_s - "OperatorInstruction: #{left} #{operator} #{right}" + class_source "#{left} #{operator} #{right}" end end diff --git a/lib/risc/instructions/setter.rb b/lib/risc/instructions/setter.rb index 67b446a8..97100ba1 100644 --- a/lib/risc/instructions/setter.rb +++ b/lib/risc/instructions/setter.rb @@ -15,7 +15,7 @@ module Risc # If you had a c array and index offset # the instruction would do array[index] = register # The arguments are in the order that makes sense for the Instruction name - # So RegToSlot means the register (first argument) moves to the slot (array and index) + # So RegToSlot means the register (first argument) moves to the slot (array and index) def initialize( source , register , array , index ) super(source) @register = register @@ -29,7 +29,7 @@ module Risc attr_accessor :register , :array , :index def to_s - "#{self.class.name.split("::").last}: #{register} -> #{array}[#{index}]" + class_source "#{register} -> #{array}[#{index}]" end end diff --git a/lib/risc/instructions/syscall.rb b/lib/risc/instructions/syscall.rb index dc72511b..b612004c 100644 --- a/lib/risc/instructions/syscall.rb +++ b/lib/risc/instructions/syscall.rb @@ -12,11 +12,12 @@ module Risc def initialize source ,name super(source) @name = name + raise "must have name" unless name end attr_reader :name def to_s - "Syscall: #{name}" + class_source name end end diff --git a/lib/risc/instructions/transfer.rb b/lib/risc/instructions/transfer.rb index fc955a49..78c6930f 100644 --- a/lib/risc/instructions/transfer.rb +++ b/lib/risc/instructions/transfer.rb @@ -26,7 +26,7 @@ module Risc attr_reader :from, :to def to_s - "Transfer: #{from} -> #{to}" + class_source "#{from} -> #{to}" end end def self.transfer( source , from , to)