2014-05-06 11:47:07 +02:00
|
|
|
module Core
|
2014-05-06 11:42:43 +02:00
|
|
|
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).
|
2014-05-06 11:42:43 +02:00
|
|
|
module ClassMethods
|
2014-05-13 17:21:24 +02:00
|
|
|
def main_start block
|
2014-05-06 11:42:43 +02:00
|
|
|
#TODO extract args into array of strings
|
2014-05-13 17:21:24 +02:00
|
|
|
Vm::CMachine.instance.main_start block
|
2014-05-14 09:47:30 +02:00
|
|
|
block
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def main_exit block
|
2014-05-06 11:42:43 +02:00
|
|
|
# Machine.exit mov r7 , 0 + swi 0
|
2014-05-13 17:21:24 +02:00
|
|
|
Vm::CMachine.instance.main_exit block
|
2014-05-14 09:47:30 +02:00
|
|
|
block
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def function_entry block , f_name
|
|
|
|
Vm::CMachine.instance.function_entry block , f_name
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def function_exit block , f_name
|
|
|
|
Vm::CMachine.instance.function_exit block , f_name
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-13 20:06:12 +02:00
|
|
|
|
|
|
|
#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
|
2014-05-15 15:54:23 +02:00
|
|
|
def putstring context
|
2014-05-13 20:06:12 +02:00
|
|
|
function = Vm::Function.new(:putstring , [Vm::Integer , Vm::Integer ] )
|
|
|
|
block = function.body
|
|
|
|
# should be another level of indirection, ie write(io,str)
|
|
|
|
ret = Vm::CMachine.instance.write_stdout(block)
|
|
|
|
function.return_type = ret
|
|
|
|
function
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-15 15:54:23 +02:00
|
|
|
|
|
|
|
def putint context
|
|
|
|
function = Vm::Function.new(:putint , [Vm::Integer , Vm::Integer ] )
|
|
|
|
block = function.body
|
|
|
|
buffer = Vm::StringConstant.new(" ")
|
|
|
|
context.program.add_object buffer
|
2014-05-16 18:55:46 +02:00
|
|
|
str_addr = Vm::Integer.new(0) # address of the
|
|
|
|
block.add_code Vm::CMachine.instance.mov( str_addr , right: Vm::Integer.new(1)) #move arg up
|
|
|
|
block.add_code Vm::CMachine.instance.add( str_addr , left: buffer) # string to write to
|
|
|
|
context.str_addr = str_addr
|
|
|
|
itos_fun = context.program.get_or_create_function(:utoa)
|
|
|
|
block.add_code Vm::CMachine.instance.call( itos_fun , {})
|
|
|
|
# function.return_type = ret
|
2014-05-15 15:54:23 +02:00
|
|
|
function
|
|
|
|
end
|
2014-05-16 18:55:46 +02:00
|
|
|
|
|
|
|
def utoa context
|
|
|
|
function = Vm::Function.new(:utoa , [Vm::Integer , Vm::Integer ] )
|
|
|
|
block = function.body
|
|
|
|
str_addr = context.str_addr
|
|
|
|
number = Vm::Integer.new(str_addr.register + 1)
|
|
|
|
remainder = Vm::Integer.new( number.register + 1)
|
|
|
|
#STMFD sp!, {r9, r10, lr} #function entry save working regs (for recursion)
|
|
|
|
#automatic block.add_code push( [:lr ] , {} ) #and the return address.
|
|
|
|
# MOV r9, r1 # preserve arguments over following
|
|
|
|
# MOV r10, r2 # function calls
|
|
|
|
# BL udiv10 # r1 = r1 / 10
|
|
|
|
Vm::CMachine.instance.div10( block , number , remainder )
|
|
|
|
# ADD r10, r10, 48 #'0' # make char out of digit (by using ascii encoding)
|
|
|
|
block.add_code Vm::CMachine.instance.add( remainder , left: remainder , right: 48 )
|
|
|
|
#STRB r10, [r1], 1 # store digit at end of buffer
|
|
|
|
block.add_code Vm::CMachine.instance.strb( remainder , right: str_addr ) #and increment TODO check
|
|
|
|
# CMP r1, #0 # quotient non-zero?
|
|
|
|
block.add_code Vm::CMachine.instance.cmp( number , right: 0 )
|
|
|
|
#BLNE utoa # conditional recursive call to utoa
|
|
|
|
block.add_code Vm::CMachine.instance.callne( function , {} )
|
|
|
|
#LDMFD sp!, {r9, r10, pc} # function exit - restore and return
|
|
|
|
#automatic block.add_code pop( [:pc] , {} )
|
|
|
|
return function
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-05-02 07:02:25 +02:00
|
|
|
end
|
2014-05-06 11:42:43 +02:00
|
|
|
|
|
|
|
extend ClassMethods
|
2014-05-02 07:02:25 +02:00
|
|
|
end
|
|
|
|
end
|