fixed while compilation (block order) and fixed and improved fibo

This commit is contained in:
Torsten Ruger 2014-06-12 16:23:57 +03:00
parent 8da1bc4645
commit ea0572c836
4 changed files with 28 additions and 29 deletions

View File

@ -97,8 +97,8 @@ module Arm
def write_stdout function #, string def write_stdout function #, string
# TODO save and restore r0 # TODO save and restore r0
function.mov( :r0 , 1 ) # 1 == stdout function.mov( :r0 , 1 ) # 1 == stdout
function.mov( :r1 , RECEIVER_REG ) function.mov( :r1 , receiver_register )
function.mov( RECEIVER_REG , :r3 ) function.mov( receiver_register , :r3 )
syscall( function.insertion_point , 4 ) # 4 == write syscall( function.insertion_point , 4 ) # 4 == write
end end

View File

@ -3,8 +3,8 @@ module Ast
# attr_reader :condition, :body # attr_reader :condition, :body
def compile context def compile context
into = context.function into = context.function
while_block = into.new_block "while" ret = into.new_block "while_end"
ret = into.new_block "return" while_block = into.new_block "while_start"
into.insert_at while_block into.insert_at while_block
puts "compiling while condition #{condition}" puts "compiling while condition #{condition}"

View File

@ -56,40 +56,40 @@ module Core
return utoa_function return utoa_function
end end
# testing method, hand coded fibo, expects arg in 1 # testing method, hand coded fibo, expects arg in receiver_register
# result comes in 7 # result comes in return_register
# 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 http://www.peter-cockerell.net/aalp/html/ch-5.html
def fibo context def fibo context
fibo_function = Vm::Function.new(:fibo , Vm::Integer , [] , Vm::Integer ) fibo_function = Vm::Function.new(:fibo , Vm::Integer , [] , Vm::Integer )
result = fibo_function.return_type result = fibo_function.return_type
int = fibo_function.receiver int = fibo_function.receiver
count = fibo_function.new_local
last = fibo_function.new_block("return")
f1 = fibo_function.new_local f1 = fibo_function.new_local
f2 = fibo_function.new_local f2 = fibo_function.new_local
fibo_function.instance_eval do
fibo_function.instance_eval do
cmp int , 1 cmp int , 1
mov( result, int , condition_code: :le) mov( result, int , condition_code: :le)
mov( :pc , :lr , condition_code: :le) ble( last ) #branch to return, rather than return (as the original)
push [ count , f1 , f2 , :lr] mov f1 , 1 #set up initial values
mov f1 , 1
mov f2 , 0 mov f2 , 0
sub count , int , 2
end end
l = fibo_function.new_block("loop") loop = fibo_function.new_block("loop")
fibo_function.insert_at l fibo_function.insert_at loop
fibo_function.instance_eval do fibo_function.instance_eval do #loop through
add f1 , f1 , f2 add f1 , f1 , f2 # f1 = f1 + f2
sub f2 , f1 , f2 sub f2 , f1 , f2 # f2 = f1 -f2
sub count , count , 1 , set_update_status: 1 sub int , int , 1 # todo: set.. should do below cmp, but doesn't , set_update_status: 1
bpl( l ) cmp int , 1
bne( loop )
mov( result , f1 ) mov( result , f1 )
pop [ count , f1 , f2 , :pc]
end end
fibo_function.set_return result
fibo_function fibo_function
end end

View File

@ -20,7 +20,7 @@ end # r0 <- r5
fibonaccit( 10 ) fibonaccit( 10 )
HERE HERE
@should = [0x0,0x40,0x2d,0xe9,0x0,0x20,0xa0,0xe3,0x1,0x30,0xa0,0xe3,0x1,0x0,0x51,0xe3,0x6,0x0,0x0,0xda,0x2,0x40,0xa0,0xe1,0x3,0x20,0xa0,0xe1,0x3,0x50,0x84,0xe0,0x5,0x30,0xa0,0xe1,0x1,0x60,0x41,0xe2,0x6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x20,0xa0,0xe3,0x1,0x30,0xa0,0xe3,0x1,0x0,0x51,0xe3,0x6,0x0,0x0,0xda,0x2,0x40,0xa0,0xe1,0x3,0x20,0xa0,0xe1,0x3,0x50,0x84,0xe0,0x5,0x30,0xa0,0xe1,0x1,0x60,0x41,0xe2,0x6,0x10,0xa0,0xe1,0xf6,0xff,0xff,0xea,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8] @should = [0x0,0x40,0x2d,0xe9,0x0,0x40,0xa0,0xe3,0x1,0x50,0xa0,0xe3,0x1,0x0,0x53,0xe3,0x4,0x0,0x0,0xda,0x4,0x60,0xa0,0xe1,0x5,0x40,0xa0,0xe1,0x5,0x50,0x86,0xe0,0x1,0x30,0x43,0xe2,0xf8,0xff,0xff,0xea,0x20,0x0,0x2d,0xe9,0x5,0x20,0xa0,0xe1,0x13,0x0,0x0,0xeb,0x20,0x0,0xbd,0xe8,0x5,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
@output = " 55 " @output = " 55 "
@target = [:Object , :fibonaccit] @target = [:Object , :fibonaccit]
parse parse
@ -30,16 +30,15 @@ HERE
# 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_kernel_fibo def test_kernel_fibo
int = Vm::Integer.new(1) int = Vm::Integer.new(Vm::RegisterMachine.instance.receiver_register)
fibo = @object_space.get_or_create_class(:Object).get_or_create_function(:fibo) fibo = @object_space.get_or_create_class(:Object).get_or_create_function(:fibo)
#HACk this with send
main = @object_space.main main = @object_space.main
main.mov int , 10 main.mov int , 10
ret = main.call( fibo ) main.call( fibo )
main.mov( :r1 , :r7 ) main.mov( Vm::RegisterMachine.instance.receiver_register , Vm::RegisterMachine.instance.return_register )
putint = @object_space.get_or_create_class(:Object).get_or_create_function(:putint) putint = @object_space.get_or_create_class(:Object).get_or_create_function(:putint)
@object_space.main.call( putint ) main.call( putint )
@should = [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,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] @should = [0x0,0x40,0x2d,0xe9,0x1,0x0,0x52,0xe3,0x2,0x0,0xa0,0xd1,0x7,0x0,0x0,0xda,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x42,0xe2,0x1,0x0,0x52,0xe3,0xfa,0xff,0xff,0x1a,0x3,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
@target = [:Object , :fibo] @target = [:Object , :fibo]
write "fibo" write "fibo"
end end