2014-06-13 23:19:12 +02:00
|
|
|
#integer related kernel functions
|
2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
2015-06-29 20:03:58 +02:00
|
|
|
module Builtin
|
|
|
|
module Integer
|
|
|
|
module ClassMethods
|
2015-10-18 16:20:19 +02:00
|
|
|
include AST::Sexp
|
2018-03-18 17:38:35 +01:00
|
|
|
include CompileHelper
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2015-11-13 19:46:27 +01:00
|
|
|
def mod4 context
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Integer,:mod4 ,{})
|
2015-10-28 20:37:42 +01:00
|
|
|
return compiler.method
|
2015-08-05 17:49:37 +02:00
|
|
|
end
|
2015-11-11 19:41:02 +01:00
|
|
|
def putint context
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Integer,:putint ,{})
|
2015-10-28 20:37:42 +01:00
|
|
|
return compiler.method
|
2015-08-05 17:49:37 +02:00
|
|
|
end
|
2015-11-11 19:41:02 +01:00
|
|
|
|
2018-03-30 16:09:02 +02:00
|
|
|
def +( context )
|
|
|
|
source = "plus"
|
2018-03-31 18:17:55 +02:00
|
|
|
compiler = compiler_for(:Integer,:+ ,{other: :Integer})
|
2018-03-30 17:05:38 +02:00
|
|
|
me , other = self_and_int_arg(compiler,source + "1")
|
2018-03-30 16:09:02 +02:00
|
|
|
# reduce me and other to integers
|
2018-03-31 18:37:24 +02:00
|
|
|
compiler.add_slot_to_reg( source + "2" , me , Parfait::Integer.integer_index , me)
|
|
|
|
compiler.add_slot_to_reg( source + "3", other , Parfait::Integer.integer_index , other)
|
2018-03-30 17:05:38 +02:00
|
|
|
compiler.add_code Risc.op( source + "4", :+ , me , other)
|
2018-04-01 13:01:17 +02:00
|
|
|
compiler.add_new_int(me , other)
|
2018-03-31 19:21:27 +02:00
|
|
|
compiler.add_reg_to_slot( source + "5" , other , :message , :return_value)
|
2018-03-30 16:09:02 +02:00
|
|
|
return compiler.method
|
2015-11-21 13:20:25 +01:00
|
|
|
|
2018-03-30 16:09:02 +02:00
|
|
|
end
|
2018-03-24 15:51:26 +01:00
|
|
|
def div10( context )
|
2015-11-21 13:20:25 +01:00
|
|
|
s = "div_10"
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Integer,:div10 ,{})
|
2018-03-14 15:56:13 +01:00
|
|
|
me = compiler.add_known( :receiver )
|
|
|
|
tmp = compiler.add_known( :receiver )
|
|
|
|
q = compiler.add_known( :receiver )
|
2018-03-11 12:32:42 +01:00
|
|
|
const = compiler.use_reg :Integer , 1
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s, 1 , const )
|
2015-11-21 13:20:25 +01:00
|
|
|
# int tmp = self >> 1
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# int q = self >> 2
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 2 , const)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :>> , q , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q >> 4
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 4 , const)
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_transfer( s, q , tmp)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q >> 8
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 8 , const)
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_transfer( s, q , tmp)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q >> 16
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 16 , const)
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_transfer( s, q , tmp)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q >> 3
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 3 , const)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :>> , q , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q * 10
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 10 , const)
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_transfer( s, q , tmp)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :* , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = self - tmp
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :- , me , tmp )
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_transfer( s , me , tmp)
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = tmp + 6
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 6 , const)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :+ , tmp , const )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = tmp >> 4
|
2018-03-31 11:38:30 +02:00
|
|
|
compiler.add_load_data( s , 4 , const)
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :>> , tmp , const )
|
2015-11-21 13:20:25 +01:00
|
|
|
# return q + tmp
|
2018-03-24 16:53:27 +01:00
|
|
|
compiler.add_code Risc.op( s , :+ , q , tmp )
|
2018-04-01 13:01:17 +02:00
|
|
|
|
|
|
|
compiler.add_new_int(me , other)
|
|
|
|
compiler.add_reg_to_slot( source + "5" , other , :message , :return_value)
|
|
|
|
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_reg_to_slot( s , q , :message , :return_value)
|
2018-03-24 15:51:26 +01:00
|
|
|
compiler.add_mom( Mom::ReturnSequence.new)
|
2015-11-21 13:20:25 +01:00
|
|
|
return compiler.method
|
|
|
|
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
|