2014-06-13 23:19:12 +02:00
|
|
|
#integer related kernel functions
|
2015-06-29 20:03:58 +02:00
|
|
|
module Register
|
|
|
|
module Builtin
|
|
|
|
module Integer
|
|
|
|
module ClassMethods
|
2015-10-18 16:20:19 +02:00
|
|
|
include AST::Sexp
|
2015-08-04 20:46:33 +02:00
|
|
|
def plus c
|
2015-10-22 17:16:29 +02:00
|
|
|
plus_function = MethodSource.create_method(:Integer,:Integer,:plus , [:Integer] )
|
2015-10-14 15:16:03 +02:00
|
|
|
plus_function.source.set_return_type :Integer
|
|
|
|
plus_function.source.receiver = :Integer
|
2015-08-08 23:53:04 +02:00
|
|
|
|
2015-10-14 15:16:03 +02:00
|
|
|
tmp = Register.tmp_reg :Integer
|
2015-08-07 15:46:55 +02:00
|
|
|
index = Register.arg_index 1
|
|
|
|
plus_function.source.add_code Register.get_slot( plus_function , :message , index , tmp )
|
2015-10-18 16:20:19 +02:00
|
|
|
|
|
|
|
me = Register.tmp_reg :Integer
|
|
|
|
plus_function.source.add_code Register.get_slot(plus_function , :message , :receiver , me )
|
2015-10-22 17:16:29 +02:00
|
|
|
add = OperatorInstruction.new( plus_function, :add , me , tmp )
|
2015-08-07 15:46:55 +02:00
|
|
|
plus_function.source.add_code add
|
2015-10-18 16:20:19 +02:00
|
|
|
plus_function.source.add_code Register.set_slot(plus_function , me , :message , :return_value )
|
2015-08-04 20:46:33 +02:00
|
|
|
return plus_function
|
|
|
|
end
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2015-08-05 17:49:37 +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
|
|
|
|
# arguments: string address , integer
|
|
|
|
# def utoa context
|
2015-10-22 17:16:29 +02:00
|
|
|
# utoa_function = MethodSource.create_method(:Integer ,:utoa , [ :Integer ] )
|
2015-10-14 15:16:03 +02:00
|
|
|
# function.source.return_type = :Integer
|
|
|
|
# function.source.receiver = :Integer
|
2015-08-05 17:49:37 +02:00
|
|
|
# return utoa_function
|
|
|
|
# # str_addr = utoa_function.receiver
|
|
|
|
# # number = utoa_function.args.first
|
|
|
|
# # remainder = utoa_function.new_local
|
2015-10-22 17:16:29 +02:00
|
|
|
# # RegisterMachine.instance.div10( utoa_function , number , remainder )
|
2015-08-05 17:49:37 +02:00
|
|
|
# # # make char out of digit (by using ascii encoding) 48 == "0"
|
|
|
|
# # utoa_function.instance_eval do
|
|
|
|
# # add( remainder , remainder , 48)
|
|
|
|
# # strb( remainder, str_addr )
|
|
|
|
# # sub( str_addr, str_addr , 1 )
|
|
|
|
# # cmp( number , 0 )
|
|
|
|
# # callne( utoa_function )
|
|
|
|
# # end
|
|
|
|
# # return utoa_function
|
2015-07-21 18:57:38 +02:00
|
|
|
# end
|
2014-06-13 23:19:12 +02:00
|
|
|
|
2015-08-05 17:49:37 +02:00
|
|
|
def putint context
|
2015-10-22 17:16:29 +02:00
|
|
|
putint_function = MethodSource.create_method(:Integer,:Integer,:putint , [] )
|
2015-10-14 15:16:03 +02:00
|
|
|
putint_function.source.set_return_type :Integer
|
|
|
|
putint_function.source.receiver = :Integer
|
2015-08-05 17:49:37 +02:00
|
|
|
return putint_function
|
|
|
|
# buffer = Parfait::Word.new(" ") # create a buffer
|
|
|
|
# context.object_space.add_object buffer # and save it (function local variable: a no no)
|
|
|
|
# int = putint_function.receiver
|
|
|
|
# moved_int = putint_function.new_local
|
|
|
|
# utoa = context.object_space.get_class_by_name(:Object).resolve_method(:utoa)
|
|
|
|
# putint_function.instance_eval do
|
|
|
|
# mov( moved_int , int ) # move arg up
|
|
|
|
# add( int , buffer ,nil ) # string to write to (add string address to pc)
|
|
|
|
# add( int , int , buffer.length - 3) # 3 for good measure , ahem.
|
|
|
|
# call( utoa )
|
|
|
|
# after = new_block("after_call")
|
|
|
|
# insert_at after
|
|
|
|
# # And now we "just" have to print it, using the write_stdout
|
|
|
|
# add( int , buffer , nil ) # string to write to
|
|
|
|
# mov( moved_int , buffer.length )
|
|
|
|
# end
|
2015-10-22 17:16:29 +02:00
|
|
|
# RegisterMachine.instance.write_stdout(putint_function)
|
2015-08-05 17:49:37 +02:00
|
|
|
# putint_function
|
|
|
|
end
|
|
|
|
|
|
|
|
# testing method, hand coded fibo, expects arg in receiver_register
|
|
|
|
# result comes in return_register
|
|
|
|
# a hand coded version of the fibonachi numbers
|
|
|
|
# not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html
|
|
|
|
def fibo context
|
2015-10-22 17:16:29 +02:00
|
|
|
fibo_function = MethodSource.create_method(:Integer,:Integer,:fibo , [] )
|
2015-10-14 15:16:03 +02:00
|
|
|
fibo_function.source.set_return_type :Integer
|
|
|
|
fibo_function.source.receiver = :Integer
|
2015-08-05 17:49:37 +02:00
|
|
|
return fibo_function
|
|
|
|
# result = fibo_function.return_type
|
|
|
|
# int = fibo_function.receiver
|
|
|
|
#
|
|
|
|
# last = fibo_function.new_block("return")
|
|
|
|
#
|
|
|
|
# f1 = fibo_function.new_local
|
|
|
|
# f2 = fibo_function.new_local
|
|
|
|
#
|
|
|
|
# fibo_function.instance_eval do
|
|
|
|
# cmp int , 1
|
|
|
|
# mov( result, int , condition_code: :le)
|
|
|
|
# ble( last ) #branch to return, rather than return (as the original)
|
|
|
|
# mov f1 , 1 #set up initial values
|
|
|
|
# mov f2 , 0
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# loop = fibo_function.new_block("loop")
|
|
|
|
# fibo_function.insert_at loop
|
|
|
|
#
|
|
|
|
# fibo_function.instance_eval do #loop through
|
|
|
|
# add f1 , f1 , f2 # f1 = f1 + f2
|
|
|
|
# sub f2 , f1 , f2 # f2 = f1 -f2
|
|
|
|
# sub int , int , 1 # todo: set.. should do below cmp, but doesn't , set_update_status: 1
|
|
|
|
# cmp int , 1
|
|
|
|
# bne( loop )
|
|
|
|
# mov( result , f1 )
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# fibo_function
|
|
|
|
end
|
2015-06-29 20:03:58 +02:00
|
|
|
end
|
|
|
|
extend ClassMethods
|
2014-09-11 14:08:56 +02:00
|
|
|
end
|
2014-06-13 23:19:12 +02:00
|
|
|
end
|
2015-05-24 12:31:33 +02:00
|
|
|
end
|