diff --git a/lib/elf/text_section.rb b/lib/elf/text_section.rb index fd28b690..a787c504 100644 --- a/lib/elf/text_section.rb +++ b/lib/elf/text_section.rb @@ -14,6 +14,12 @@ module Elf Elf::Constants::SHF_WRITE | Elf::Constants::SHF_ALLOC | Elf::Constants::SHF_EXECINSTR end + def length + @text.length + end + def to_s + "[" + @text.bytes.join(",") + "]" + end def alignment 4 end diff --git a/lib/vm/block.rb b/lib/vm/block.rb index 74e7459a..85f72415 100644 --- a/lib/vm/block.rb +++ b/lib/vm/block.rb @@ -63,6 +63,9 @@ module Vm end end + def at where + @current = where + end # set the next executed block after self. # why is this useful? if it's unconditional, why not merge them: # So the second block can be used as a jump target. You standard loop needs a block to setup @@ -74,7 +77,10 @@ module Vm # 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) - raise "hallo" if( meth.to_s[-1] == "=") + if( meth.to_s[-1] == "=") + val = @current.eval meth.to_s[0 ... -1] + raise "hallo #{val}" + end add_code CMachine.instance.send(meth , *args) end diff --git a/test/arm/test_fibo.rb b/test/arm/test_fibo.rb index a8c85c72..66b4e9b8 100644 --- a/test/arm/test_fibo.rb +++ b/test/arm/test_fibo.rb @@ -13,8 +13,8 @@ class TestFibo < MiniTest::Test fibo = @program.get_or_create_function(:fibo) @program.main.mov( int , 10 ) @program.main.call( fibo ) - putint = @program.get_or_create_function(:putint) - @program.main.call( putint ) +# putint = @program.get_or_create_function(:putint) +# @program.main.call( putint ) write 20 , "fibo" end diff --git a/test/arm/test_small_program.rb b/test/arm/test_small_program.rb index 6ebac49c..6d58991e 100644 --- a/test/arm/test_small_program.rb +++ b/test/arm/test_small_program.rb @@ -22,6 +22,7 @@ class TestSmallProg < MiniTest::Test bne start ,{} #3 end end + @should = [0,176,160,227,5,0,160,227,1,0,80,226,253,255,255,26,1,112,160,227,0,0,0,239] write( 6 , "loop" ) end @@ -35,17 +36,18 @@ class TestSmallProg < MiniTest::Test mov :r2 , hello.length swi 0 #software interupt, ie kernel syscall end - write(7 + hello.length/4 + 1 , 'hello') + @should = [0,176,160,227,4,112,160,227,1,0,160,227,12,16,143,226,16,32,160,227,0,0,0,239,1,112,160,227,0,0,0,239,72,101,108,108,111,32,82,97,105,115,97,10,0,0,0,0] + write(7 + hello.length/4 + 1 , 'hello') end #helper to write the file def write len ,name - writer = Elf::ObjectWriter.new(Elf::Constants::TARGET_ARM) - io = @program.assemble(StringIO.new) - assembly = io.string - assert_equal len * 4 , assembly.length - writer.set_text assembly - writer.save("#{name}_test.o") + writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM) + assembly = writer.text + assembly.text.bytes.each_with_index do |byte , index| + assert_equal byte , @should[index] , "byte #{index}" + end +# writer.save("#{name}_test.o") end end # _start copied from dietc