fix the handcoded fibonacci (registers again)

This commit is contained in:
Torsten Ruger 2014-05-25 13:34:06 +03:00
parent 5afa6f4239
commit 1f17262887
2 changed files with 18 additions and 20 deletions

View File

@ -74,16 +74,15 @@ module Core
return utoa_function return utoa_function
end end
# testing method, hand coded fibo, expects arg in 1 , so pass 2 in, first bogy # testing method, hand coded fibo, expects arg in 1
# result comes in 0 # result comes in 7
# a hand coded version of the fibonachi numbers # a hand coded version of the fibonachi numbers
# not my hand off course, found in the net from a basic introduction # not my hand off course, found in the net from a basic introduction
def fibo context def fibo context
fibo_function = Vm::Function.new(:fibo , [Vm::Integer , Vm::Integer] , Vm::Integer ) fibo_function = Vm::Function.new(:fibo , [Vm::Integer] , Vm::Integer )
result = fibo_function.args[0] result = Vm::Integer.new(7)
int = fibo_function.args[1] int = fibo_function.args[0]
count = fibo_function.new_local count = fibo_function.new_local
loop_block = Vm::Block.new("loop", nil)
f1 = fibo_function.new_local f1 = fibo_function.new_local
f2 = fibo_function.new_local f2 = fibo_function.new_local
b = fibo_function.body.scope binding b = fibo_function.body.scope binding
@ -96,14 +95,14 @@ module Core
b.f2 = 0 b.f2 = 0
b.count = int - 2 b.count = int - 2
b.add_code loop_block l = fibo_function.body.new_block("loop").scope binding
l = loop_block.scope binding
l.f1 = f1 + f2 l.f1 = f1 + f2
l.f2 = f1 - f2 l.f2 = f1 - f2
l.count = (count - 1).set_update_status l.count = (count - 1).set_update_status
l.bpl( loop_block ) l.bpl( l )
l.result = f1 l.mov( result , f1 )
fibo_function.set_return result
l.pop [ count , f1 , f2 , :pc] l.pop [ count , f1 , f2 , :pc]
fibo_function fibo_function
end end

View File

@ -38,24 +38,22 @@ class TestSmallProg < MiniTest::Test
b.r1 = hello # address of "hello Raisa" b.r1 = hello # address of "hello Raisa"
b.r2 = hello.length b.r2 = hello.length
b.swi 0 #software interupt, ie kernel syscall b.swi 0 #software interupt, ie kernel syscall
@should = [0x0,0xb0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0xc,0x10,0x8f,0xe2,0x10,0x20,0xa0,0xe3,0x0,0x0,0x0,0xef,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x52,0x61,0x69,0x73,0x61,0xa,0x0,0x0,0x0,0x0] @should = [0x0,0xb0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x10,0x10,0x8f,0xe2,0x10,0x20,0xa0,0xe3,0x0,0x0,0x0,0xef,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x52,0x61,0x69,0x73,0x61,0xa,0x0,0x0,0x0,0x0]
write "hello" write "hello"
end end
# a hand coded version of the fibonachi numbers (moved to kernel to be able to call it) # a hand coded version of the fibonachi numbers (moved to kernel to be able to call it)
# not my hand off course, found in the net from a basic introduction # not my hand off course, found in the net from a basic introduction
def test_fibo def test_fibo
int = Vm::Integer.new(1) # the one is funny, but the fibo is _really_ tight code and reuses registers int = Vm::Integer.new(1)
fibo = @program.get_or_create_function(:fibo) fibo = @program.get_or_create_function(:fibo)
main= @program.main.scope binding main = @program.main.scope binding
main.int = 10 main.int = 10
main.call( fibo ) ret = main.call( fibo )
# this is the version without the putint (which makes the program 3 times bigger) main.mov( :r1 , :r7 )
@should = [0x0,0xb0,0xa0,0xe3,0xa,0x10,0xa0,0xe3,0x1,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x40,0x2d,0xe9,0x1,0x0,0x51,0xe3,0x1,0x0,0xa0,0xd1,0xe,0xf0,0xa0,0xd1,0x1c,0x40,0x2d,0xe9,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x2,0x20,0x41,0xe2,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x52,0xe2,0xfb,0xff,0xff,0x5a,0x3,0x0,0xa0,0xe1,0x1c,0x80,0xbd,0xe8,0x0,0x80,0xbd,0xe8]
putint = @program.get_or_create_function(:putint) putint = @program.get_or_create_function(:putint)
@program.main.call( putint ) @program.main.call( putint )
# so here the "full" version with putint @should = [0x0,0xb0,0xa0,0xe3,0xa,0x10,0xa0,0xe3,0x4,0x0,0x0,0xeb,0x7,0x10,0xa0,0xe1,0x22,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x1,0x0,0x51,0xe3,0x1,0x70,0xa0,0xd1,0xe,0xf0,0xa0,0xd1,0x1c,0x40,0x2d,0xe9,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x2,0x20,0x41,0xe2,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x52,0xe2,0xfb,0xff,0xff,0x5a,0x3,0x70,0xa0,0xe1,0x1c,0x80,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x0]
@should = [0,176,160,227,10,16,160,227,2,0,0,235,33,0,0,235,1,112,160,227,0,0,0,239,0,64,45,233,1,0,81,227,1,0,160,209,14,240,160,209,28,64,45,233,1,48,160,227,0,64,160,227,2,32,65,226,4,48,131,224,4,64,67,224,1,32,82,226,251,255,255,90,3,0,160,225,28,128,189,232,0,128,189,232,0,64,45,233,10,32,65,226,33,17,65,224,33,18,129,224,33,20,129,224,33,24,129,224,161,17,160,225,1,49,129,224,131,32,82,224,1,16,129,82,10,32,130,66,48,32,130,226,0,32,192,229,1,0,64,226,0,0,81,227,239,255,255,27,0,128,189,232,0,64,45,233,0,16,160,225,36,0,143,226,9,0,128,226,233,255,255,235,24,0,143,226,12,16,160,227,1,32,160,225,0,16,160,225,1,0,160,227,4,112,160,227,0,0,0,239,0,128,189,232,32,32,32,32,32,32,32,32,32,32,32,0]
write "fibo" write "fibo"
end end
@ -64,10 +62,11 @@ class TestSmallProg < MiniTest::Test
writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM) writer = Elf::ObjectWriter.new(@program , Elf::Constants::TARGET_ARM)
assembly = writer.text assembly = writer.text
# use this for getting the bytes to compare to : # use this for getting the bytes to compare to :
# puts assembly puts assembly
#writer.save("#{name}_test.o") writer.save("#{name}_test.o")
assembly.text.bytes.each_with_index do |byte , index| assembly.text.bytes.each_with_index do |byte , index|
is = @should[index] is = @should[index]
assert_equal is.class, Fixnum
assert_equal byte , is , "@#{index.to_s(16)} #{byte.to_s(16)} != #{is.to_s(16)}" assert_equal byte , is , "@#{index.to_s(16)} #{byte.to_s(16)} != #{is.to_s(16)}"
end end
end end