adds hand coded fibo, works

This commit is contained in:
Torsten Ruger
2014-05-19 17:32:41 +03:00
parent 285988f173
commit fe1414f383
8 changed files with 76 additions and 15 deletions

View File

@ -4,3 +4,4 @@ require_relative "test_logic"
require_relative "test_move"
require_relative "test_memory"
require_relative "test_compare"
require_relative "test_small_program"

26
test/arm/test_fibo.rb Normal file
View File

@ -0,0 +1,26 @@
require_relative 'helper'
class TestFibo < MiniTest::Test
def setup
@program = Vm::Program.new "Arm"
end
# a hand coded version of the fibonachi numbers
# not my hand off course, found in the net from a basic introduction
def test_fibo
int = Vm::Integer.new(1) # the one is funny, but the fibo is _really_ tight code and reuses registers
fibo = utoa = @program.get_or_create_function(:fibo)
@program.main.mov( int , 10 )
@program.main.call( fibo )
#could put ...
write 20 , "fibo"
end
#helper to write the file
def write len ,name
writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM)
writer.save("#{name}_test.o")
end
end

View File

@ -0,0 +1,59 @@
require_relative 'helper'
# test the generation of a whole program
# not many asserts, but assume all is well (ho ho)
# linking and running does not produce seqmentation fault, ie it works
# should really thiink about more asserts (but currently no way to execute as still running on mac )
# (have to fix the int being used in code generation as ruby int is only 31 bits, and that wont do)
class TestSmallProg < MiniTest::Test
# need a code generator, for arm
def setup
@program = Vm::Program.new "Arm"
end
def test_loop
@program.main.instance_eval do
mov :r0, 5 #1
start = Vm::Block.new("start")
add_code start
start.instance_eval do
sub :r0, :r0, 1 , :update_status => 1 #2
bne start ,{} #3
end
end
write( 6 , "loop" )
end
def test_hello
hello = Vm::StringConstant.new "Hello Raisa\n"
@program.add_object hello
@program.main.instance_eval do
mov :r7, 4 # 4 == write
mov :r0 , 1 # stdout
add :r1 , hello , nil # address of "hello Raisa"
mov :r2 , hello.length
swi 0 #software interupt, ie kernel syscall
end
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")
end
end
# _start copied from dietc
# mov fp, #0 @ clear the frame pointer
# ldr a1, [sp] @ argc
# add a2, sp, #4 @ argv
# ldr ip, .L3
# add a3, a2, a1, lsl #2 @ &argv[argc]
# add a3, a3, #4 @ envp
# str a3, [ip, #0] @ environ = envp
# bl main