From 53d8f4b1636eeb69cf4156e3dae0ecba91ddf41f Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 18 Jul 2015 11:21:49 +0300 Subject: [PATCH] add source to instruction for debug --- lib/register/instruction.rb | 5 +++++ lib/register/instructions/branch.rb | 11 +++++++++-- lib/register/passes/main_implementation.rb | 2 +- lib/virtual/instructions/virtual_main.rb | 4 ++++ lib/virtual/machine.rb | 2 ++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/register/instruction.rb b/lib/register/instruction.rb index 76ac2c81..d599ef8c 100644 --- a/lib/register/instruction.rb +++ b/lib/register/instruction.rb @@ -9,6 +9,11 @@ module Register # constants can/must be loaded into registers before use class Instruction + def initialize source + @source = source + end + attr_reader :block , :source + # returns an array of registers (RegisterReferences) that this instruction uses. # ie for r1 = r2 + r3 # which in assembler is add r1 , r2 , r3 diff --git a/lib/register/instructions/branch.rb b/lib/register/instructions/branch.rb index 63a50e89..26019681 100644 --- a/lib/register/instructions/branch.rb +++ b/lib/register/instructions/branch.rb @@ -3,10 +3,17 @@ module Register # a branch must branch to a block. class Branch < Instruction - def initialize to + def initialize source , to + super(source) raise "No block" unless to @block = to end - attr_reader :block + attr_reader :block + + def to_s + "Branch(to: #{block.name})" + end + end + end diff --git a/lib/register/passes/main_implementation.rb b/lib/register/passes/main_implementation.rb index bd1b2b50..c73aa07b 100644 --- a/lib/register/passes/main_implementation.rb +++ b/lib/register/passes/main_implementation.rb @@ -8,7 +8,7 @@ module Register def run block block.codes.dup.each do |code| next unless code.is_a? Virtual::VirtualMain - branch = Register::Branch.new( code.method.source.blocks.first ) + branch = Register::Branch.new( code , code.method.source.blocks.first ) block.replace(code , branch ) end end diff --git a/lib/virtual/instructions/virtual_main.rb b/lib/virtual/instructions/virtual_main.rb index cbac171b..e3eecc2d 100644 --- a/lib/virtual/instructions/virtual_main.rb +++ b/lib/virtual/instructions/virtual_main.rb @@ -15,5 +15,9 @@ module Virtual def word_length 4 end + + def to_s + "#{self.class.name}( method: #{method.name})" + end end end diff --git a/lib/virtual/machine.rb b/lib/virtual/machine.rb index 94071097..9a8020fd 100644 --- a/lib/virtual/machine.rb +++ b/lib/virtual/machine.rb @@ -35,6 +35,8 @@ module Virtual class Machine FIRST_PASS = "Virtual::SendImplementation" + LAST_PASS = "Virtual::SetOptimisation" + def initialize @parser = Parser::Salama.new @passes = [ FIRST_PASS ]