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-07 15:46:55 +02:00
|
|
|
|
2015-11-13 19:46:27 +01:00
|
|
|
def mod4 context
|
2016-12-18 13:15:19 +01:00
|
|
|
compiler = Typed::MethodCompiler.new.create_method(:Integer,:mod4 ).init_method
|
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
|
2016-12-18 13:15:19 +01:00
|
|
|
compiler = Typed::MethodCompiler.new.create_method(:Integer,:putint ).init_method
|
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
|
|
|
|
2015-11-21 13:20:25 +01:00
|
|
|
|
|
|
|
def div10 context
|
|
|
|
s = "div_10"
|
2016-12-18 13:15:19 +01:00
|
|
|
compiler = Typed::MethodCompiler.new.create_method(:Integer,:div10 ).init_method
|
2016-12-09 11:13:33 +01:00
|
|
|
me = compiler.process( Typed::Tree::NameExpression.new( :self) )
|
|
|
|
tmp = compiler.process( Typed::Tree::NameExpression.new( :self) )
|
|
|
|
q = compiler.process( Typed::Tree::NameExpression.new( :self) )
|
|
|
|
const = compiler.process( Typed::Tree::IntegerExpression.new(1) )
|
2015-11-21 13:20:25 +01:00
|
|
|
# int tmp = self >> 1
|
|
|
|
compiler.add_code Register.op( s , ">>" , tmp , const)
|
|
|
|
# int q = self >> 2
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 2 , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , ">>" , q , const)
|
|
|
|
# q = q + tmp
|
|
|
|
compiler.add_code Register.op( s , "+" , q , tmp )
|
|
|
|
# tmp = q >> 4
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 4 , const)
|
|
|
|
compiler.add_transfer( s, q , tmp)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , ">>" , tmp , const)
|
|
|
|
# q = q + tmp
|
|
|
|
compiler.add_code Register.op( s , "+" , q , tmp )
|
|
|
|
# tmp = q >> 8
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 8 , const)
|
|
|
|
compiler.add_transfer( s, q , tmp)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , ">>" , tmp , const)
|
|
|
|
# q = q + tmp
|
|
|
|
compiler.add_code Register.op( s , "+" , q , tmp )
|
|
|
|
# tmp = q >> 16
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 16 , const)
|
|
|
|
compiler.add_transfer( s, q , tmp)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , ">>" , tmp , const)
|
|
|
|
# q = q + tmp
|
|
|
|
compiler.add_code Register.op( s , "+" , q , tmp )
|
|
|
|
# q = q >> 3
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 3 , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , ">>" , q , const)
|
|
|
|
# tmp = q * 10
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 10 , const)
|
|
|
|
compiler.add_transfer( s, q , tmp)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , "*" , tmp , const)
|
|
|
|
# tmp = self - tmp
|
|
|
|
compiler.add_code Register.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
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 6 , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , "+" , tmp , const )
|
|
|
|
# tmp = tmp >> 4
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_load_constant( s , 4 , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
compiler.add_code Register.op( s , ">>" , tmp , const )
|
|
|
|
# return q + tmp
|
|
|
|
compiler.add_code Register.op( s , "+" , q , tmp )
|
2016-12-28 19:37:54 +01:00
|
|
|
compiler.add_reg_to_slot( s , q , :message , :return_value)
|
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
|