From 017d93d92996c57dd666e2d67f45b0d61aa32abc Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 12 Jun 2014 21:40:25 +0300 Subject: [PATCH] add to_asm debug output functions --- lib/vm/constants.rb | 3 +++ lib/vm/instruction.rb | 8 ++++---- lib/vm/values.rb | 5 ++++- test/fragments/test_functions.rb | 4 ++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/vm/constants.rb b/lib/vm/constants.rb index ae0bf929..ee52c711 100644 --- a/lib/vm/constants.rb +++ b/lib/vm/constants.rb @@ -22,6 +22,9 @@ module Vm def value @integer end + def to_asm + @integer.to_s + end end # The name really says it all. diff --git a/lib/vm/instruction.rb b/lib/vm/instruction.rb index f2b1f6cd..f3a6a2fa 100644 --- a/lib/vm/instruction.rb +++ b/lib/vm/instruction.rb @@ -90,7 +90,7 @@ module Vm @first end def to_s - "#{opcode} #{@first} #{super}" + "#{opcode} [#{@first.collect {|f| f.to_asm}.join(',') }] #{super}" end end class MemoryInstruction < Instruction @@ -131,8 +131,8 @@ module Vm def assigns [@result.used_register] end - def to_s - "#{opcode} #{result.register_symbol} , #{left.register_symbol} , #{right.register_symbol} #{super}" + def to_asm + "#{opcode} #{result.to_asm} , #{left.to_asm} , #{right.to_asm} #{super}" end end class CompareInstruction < Instruction @@ -165,7 +165,7 @@ module Vm [@to.used_register] end def to_s - "#{opcode} #{@to.register_symbol} , #{@from.is_a?(Constant) ? @from.value : @from.register_symbol} #{super}" + "#{opcode} #{@to.to_asm} , #{@from.to_asm} #{super}" end end class CallInstruction < Instruction diff --git a/lib/vm/values.rb b/lib/vm/values.rb index d50df05c..8a420503 100644 --- a/lib/vm/values.rb +++ b/lib/vm/values.rb @@ -103,6 +103,10 @@ module Vm def length 4 end + # aka to string + def to_asm + "#{register_symbol}" + end end class Unsigned < Word @@ -169,7 +173,6 @@ module Vm block.mov( self , right ) self end - end end require_relative "constants" \ No newline at end of file diff --git a/test/fragments/test_functions.rb b/test/fragments/test_functions.rb index 9114bcbe..59664980 100644 --- a/test/fragments/test_functions.rb +++ b/test/fragments/test_functions.rb @@ -28,9 +28,9 @@ end t_seven() HERE - @should = [0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8] + @should = [0x0,0x40,0x2d,0xe9,0x0,0x0,0x54,0xe3,0x10,0x0,0x0,0xa,0x8,0x0,0x2d,0xe9,0x4,0x30,0xa0,0xe1,0x1,0x40,0xa0,0xe3,0xf2,0xff,0xff,0xeb,0x8,0x0,0xbd,0xe8,0x0,0x50,0xa0,0xe1,0x8,0x0,0x2d,0xe9,0x5,0x40,0xa0,0xe1,0xf3,0xff,0xff,0xeb,0x8,0x0,0xbd,0xe8,0x0,0x60,0xa0,0xe1,0x10,0x0,0x2d,0xe9,0x6,0x40,0xa0,0xe1,0xeb,0xff,0xff,0xeb,0x10,0x0,0xbd,0xe8,0x0,0x30,0xa0,0xe1,0x0,0x0,0x0,0xea,0x0,0x30,0xa0,0xe3,0x3,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8] @output = " 42 " - @target = [:Object , :minus] + @target = [:Object , :times] parse write "functions" end