rubyx/lib/core/kernel.rb

135 lines
5.3 KiB
Ruby
Raw Normal View History

module Core
class Kernel
#there are no Kernel instances, only class methods.
2014-05-06 20:36:28 +02:00
# We use this module syntax to avoid the (ugly) self (also eases searching).
module ClassMethods
def main_start block
#TODO extract args into array of strings
2014-05-21 18:43:46 +02:00
Vm::RegisterMachine.instance.main_start block
block
end
def main_exit block
# Machine.exit mov r7 , 0 + swi 0
2014-05-21 18:43:46 +02:00
Vm::RegisterMachine.instance.main_exit block
block
end
def function_entry block , f_name
2014-05-21 18:43:46 +02:00
Vm::RegisterMachine.instance.function_entry block , f_name
end
def function_exit block , f_name
2014-05-21 18:43:46 +02:00
Vm::RegisterMachine.instance.function_exit block , f_name
end
# in ruby, how this goes is
# def _get_instance_variable var
# i = self.index_of(var)
# return at_index(i)
# end
# The at_index is just "below" the api, somehting we need but don't want to expose, so we can't code the above in ruby
def _get_instance_variable context , name = Vm::Integer
get_function = Vm::Function.new(:_get_instance_variable , [Vm::Integer , Vm::Integer ] , Vm::Integer )
me = get_function.args[0]
var_name = get_function.args[1]
return_to = get_function.return_type
index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of)
b = get_function.body
b.push( me )
index = b.call( index_function )
b.pop(me)
return_to.at_index( get_function.body , me , index )
get_function.set_return return_to
return get_function
end
#TODO this is in the wrong place. It is a function that returns a function object
# while all other methods add their code into some block. --> kernel
def putstring context , string = Vm::Integer , length = Vm::Integer
function = Vm::Function.new(:putstring , [string , length ] , string)
block = function.body
# should be another level of indirection, ie write(io,str)
2014-05-21 18:43:46 +02:00
ret = Vm::RegisterMachine.instance.write_stdout(block)
function.set_return ret
function
end
def putint context , arg = Vm::Integer
2014-05-19 11:18:01 +02:00
putint_function = Vm::Function.new(:putint , [arg] , arg )
buffer = Vm::StringConstant.new(" ") # create a buffer
context.object_space.add_object buffer # and save it (function local variable: a no no)
2014-05-19 11:18:01 +02:00
int = putint_function.args.first
2014-05-21 18:06:21 +02:00
moved_int = putint_function.new_local
utoa = context.object_space.get_or_create_class(:Object).get_or_create_function(:utoa)
2014-05-19 14:44:12 +02:00
b = putint_function.body
b.mov( moved_int , int ) #move arg up
#b.a buffer => int # string to write to
b.add( int , buffer ,nil ) # string to write to
b.a int + (buffer.length-3) => int
b.call( utoa )
# And now we "just" have to print it, using the write_stdout
2014-05-19 14:44:12 +02:00
b.add( int , buffer , nil ) # string to write to
b.mov( moved_int , buffer.length )
2014-05-21 18:43:46 +02:00
Vm::RegisterMachine.instance.write_stdout(putint_function.body)
2014-05-19 11:18:01 +02:00
putint_function
end
2014-05-16 18:55:46 +02:00
# The conversion to base10 is quite a bit more complicated than i thought. The bulk of it is in div10
# We set up variables, do the devision and write the result to the string
# then check if were done and recurse if neccessary
# As we write before we recurse (save a push) we write the number backwards
2014-05-19 11:18:01 +02:00
# arguments: string address , integer
2014-05-16 18:55:46 +02:00
def utoa context
2014-05-19 11:18:01 +02:00
utoa_function = Vm::Function.new(:utoa , [Vm::Integer , Vm::Integer ] , Vm::Integer )
str_addr = utoa_function.args[0]
number = utoa_function.args[1]
2014-05-21 18:06:21 +02:00
remainder = utoa_function.new_local
2014-05-21 18:43:46 +02:00
Vm::RegisterMachine.instance.div10( utoa_function.body , number , remainder )
# make char out of digit (by using ascii encoding) 48 == "0"
2014-05-21 18:06:21 +02:00
b = utoa_function.body.scope binding
b.remainder = remainder + 48
2014-05-19 14:44:12 +02:00
b.strb( remainder, right: str_addr )
b.sub( str_addr, str_addr , 1 )
b.cmp( number , 0 )
b.callne( utoa_function )
2014-05-19 11:18:01 +02:00
return utoa_function
2014-05-16 18:55:46 +02:00
end
2014-05-19 16:32:41 +02:00
# testing method, hand coded fibo, expects arg in 1
# result comes in 7
2014-05-19 16:32:41 +02:00
# a hand coded version of the fibonachi numbers
# not my hand off course, found in the net from a basic introduction
def fibo context
fibo_function = Vm::Function.new(:fibo , [Vm::Integer] , Vm::Integer )
result = Vm::Integer.new(7)
int = fibo_function.args[0]
count = fibo_function.new_local
f1 = fibo_function.new_local
f2 = fibo_function.new_local
b = fibo_function.body.scope binding
b.a int == 1
b.mov( result, int , condition_code: :le)
b.mov( :pc , :lr , condition_code: :le)
b.push [ count , f1 , f2 , :lr]
b.f1 = 1
b.f2 = 0
b.count = int - 2
l = fibo_function.body.new_block("loop").scope binding
2014-05-20 09:28:34 +02:00
l.f1 = f1 + f2
l.f2 = f1 - f2
l.count = (count - 1).set_update_status
l.bpl( l )
l.mov( result , f1 )
fibo_function.set_return result
l.pop [ count , f1 , f2 , :pc]
2014-05-19 16:32:41 +02:00
fibo_function
end
2014-05-02 07:02:25 +02:00
end
extend ClassMethods
2014-05-02 07:02:25 +02:00
end
end