From f7f0eee31a4119ebfc4543445a45d58740e9c712 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 8 May 2014 14:14:15 +0300 Subject: [PATCH] adjust program tests to new syntax, hello world works there too -) --- lib/arm/logic_instruction.rb | 2 +- lib/vm/block.rb | 10 ++++++++++ lib/vm/program.rb | 2 +- test/test_all.rb | 2 +- test/test_small_program.rb | 34 ++++++++++++++++++---------------- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/arm/logic_instruction.rb b/lib/arm/logic_instruction.rb index ea929445..f836e073 100644 --- a/lib/arm/logic_instruction.rb +++ b/lib/arm/logic_instruction.rb @@ -30,7 +30,7 @@ module Arm @operand = op_with_rot @i = 1 else - raise "cannot fit numeric literal argument in operand #{arg}" + raise "cannot fit numeric literal argument in operand #{arg.inspect}" end elsif (arg.is_a?(Symbol)) @operand = arg diff --git a/lib/vm/block.rb b/lib/vm/block.rb index d32cc8ec..80bda66f 100644 --- a/lib/vm/block.rb +++ b/lib/vm/block.rb @@ -65,6 +65,16 @@ module Vm @next = block end + # sugar to create instructions easily. Any method with one arg is sent to the machine and the result + # (hopefully an instruction) added as code + def method_missing(meth, *args, &block) + if args.length == 1 + add_code Machine.instance.send(meth , *args) + else + super + end + end + end end \ No newline at end of file diff --git a/lib/vm/program.rb b/lib/vm/program.rb index d02fead2..124e0c7c 100644 --- a/lib/vm/program.rb +++ b/lib/vm/program.rb @@ -30,7 +30,7 @@ module Vm @functions = [] @entry = Core::Kernel::main_start #main gets executed between entry and exit - @main = nil + @main = Block.new("main") @exit = Core::Kernel::main_exit end attr_reader :context , :main , :functions diff --git a/test/test_all.rb b/test/test_all.rb index ffad4450..b7632165 100644 --- a/test/test_all.rb +++ b/test/test_all.rb @@ -1,4 +1,4 @@ -require_relative "test_asm" +require_relative "test_arm" require_relative "test_small_program" #require_relative "test_runner" require_relative "parser/test_all" diff --git a/test/test_small_program.rb b/test/test_small_program.rb index fb80c458..5c4d871b 100644 --- a/test/test_small_program.rb +++ b/test/test_small_program.rb @@ -9,16 +9,16 @@ require_relative 'helper' class TestSmallProg < MiniTest::Test # need a code generator, for arm def setup - @program = Asm::Assembler.new + @program = Vm::Program.new "Arm" end def test_loop - @program.main do - mov r0, 5 #1 + @program.main.instance_eval do + mov :r0, 5 #1 start do - subs r0, r0, 1 #2 + subs :r0, :r0, 1 #2 bne :start #3 - mov r7, 1 #4 + mov :r7, 1 #4 swi 0 #5 5 instructions end end @@ -26,23 +26,25 @@ class TestSmallProg < MiniTest::Test end def test_hello - hello = "Hello Raisa\n" - @program.main do - mov r7, 4 # 4 == write - mov r0 , 1 # stdout - add r1 , pc , hello # address of "hello Raisa" - mov r2 , hello.length - swi 0 #software interupt, ie kernel syscall - mov r7, 1 # 1 == exit - swi 0 + hello = Vm::StringLiteral.new "Hello Raisa\n" + @program.add_object hello + @program.main.instance_eval do + mov :left =>:r7, :right => 4 # 4 == write + mov :left =>:r0 , :right => 1 # stdout + add :left =>:r1 , :extra => hello # address of "hello Raisa" + mov :left =>:r2 , :right => hello.length + swi :left => 0 #software interupt, ie kernel syscall + mov :left => :r7, :right => 1 # 1 == exit + swi :left => 0 end - write(7 + hello.length/4 + 1 , 'hello') + write(9 + hello.length/4 + 1 , 'hello') end #helper to write the file def write len ,name writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM) - assembly = @program.assemble_to_string + io = @program.assemble(StringIO.new) + assembly = io.string assert_equal len * 4 , assembly.length writer.set_text assembly writer.save("#{name}_test.o")