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
|
2018-03-18 17:38:35 +01:00
|
|
|
include CompileHelper
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2018-04-19 09:00:55 +02:00
|
|
|
def div4(context)
|
|
|
|
source = "div4"
|
|
|
|
compiler = compiler_for(:Integer,:div4 ,{})
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2018-04-08 17:52:17 +02:00
|
|
|
me = builder.add_known( :receiver )
|
|
|
|
builder.reduce_int( source , me )
|
2018-04-01 14:13:12 +02:00
|
|
|
two = compiler.use_reg :fixnum , 2
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( source , 2 , two )
|
|
|
|
builder.add_code Risc.op( source , :>> , me , two)
|
|
|
|
builder.add_new_int(source,me , two)
|
|
|
|
builder.add_reg_to_slot( source , two , :message , :return_value)
|
2018-04-01 14:13:12 +02:00
|
|
|
compiler.add_mom( Mom::ReturnSequence.new)
|
2015-10-28 20:37:42 +01:00
|
|
|
return compiler.method
|
2015-08-05 17:49:37 +02:00
|
|
|
end
|
2018-04-19 21:57:31 +02:00
|
|
|
def >( context )
|
2018-04-23 18:39:16 +02:00
|
|
|
comparison( :> )
|
2018-04-19 21:57:31 +02:00
|
|
|
end
|
2018-04-19 21:41:40 +02:00
|
|
|
def <( context )
|
2018-04-23 18:39:16 +02:00
|
|
|
comparison( :< )
|
2018-04-19 21:57:31 +02:00
|
|
|
end
|
2018-04-24 18:45:58 +02:00
|
|
|
def <=( context )
|
|
|
|
comparison( :<= )
|
|
|
|
end
|
|
|
|
def >=( context )
|
|
|
|
comparison( :>= )
|
|
|
|
end
|
2018-04-23 18:39:16 +02:00
|
|
|
def comparison( operator )
|
2018-04-19 21:57:31 +02:00
|
|
|
compiler = compiler_for(:Integer, operator ,{other: :Integer})
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2018-04-19 21:57:31 +02:00
|
|
|
me , other = builder.self_and_int_arg("#{operator} load receiver and arg")
|
2018-05-01 18:20:16 +02:00
|
|
|
false_label = Risc.label(compiler.method , "false_label_#{builder.object_id.to_s(16)}")
|
|
|
|
merge_label = Risc.label(compiler.method , "merge_label_#{builder.object_id.to_s(16)}")
|
2018-04-19 21:57:31 +02:00
|
|
|
builder.reduce_int( "#{operator} fix me", me )
|
|
|
|
builder.reduce_int( "#{operator} fix arg", other )
|
2018-04-24 18:45:58 +02:00
|
|
|
if(operator.to_s.start_with?('<') )
|
2018-04-23 18:39:16 +02:00
|
|
|
me , other = other , me
|
|
|
|
end
|
2018-04-19 21:57:31 +02:00
|
|
|
builder.add_code Risc.op( "#{operator} operator", :- , me , other)
|
2018-04-23 18:39:16 +02:00
|
|
|
builder.add_code IsMinus.new( "#{operator} if", false_label)
|
2018-04-24 18:45:58 +02:00
|
|
|
if(operator.to_s.length == 1)
|
|
|
|
builder.add_code IsZero.new( "#{operator} if", false_label)
|
|
|
|
end
|
2018-04-19 21:57:31 +02:00
|
|
|
builder.add_load_constant("#{operator} new int", Parfait.object_space.true_object , other)
|
2018-04-19 21:41:40 +02:00
|
|
|
builder.add_code Risc::Branch.new("jump over false", merge_label)
|
|
|
|
builder.add_code false_label
|
2018-04-19 21:57:31 +02:00
|
|
|
builder.add_load_constant("#{operator} new int", Parfait.object_space.false_object , other)
|
2018-04-19 21:41:40 +02:00
|
|
|
builder.add_code merge_label
|
2018-04-19 21:57:31 +02:00
|
|
|
builder.add_reg_to_slot( "#{operator} save ret" , other , :message , :return_value)
|
2018-04-19 21:41:40 +02:00
|
|
|
compiler.add_mom( Mom::ReturnSequence.new)
|
|
|
|
return compiler.method
|
|
|
|
end
|
2018-04-01 14:13:12 +02:00
|
|
|
def putint(context)
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Integer,:putint ,{})
|
2018-04-01 14:17:16 +02:00
|
|
|
compiler.add_mom( Mom::ReturnSequence.new)
|
2015-10-28 20:37:42 +01:00
|
|
|
return compiler.method
|
2015-08-05 17:49:37 +02:00
|
|
|
end
|
2018-04-19 21:13:52 +02:00
|
|
|
def operator_method( op_sym )
|
2018-04-01 14:26:53 +02:00
|
|
|
compiler = compiler_for(:Integer, op_sym ,{other: :Integer})
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2018-04-19 21:13:52 +02:00
|
|
|
me , other = builder.self_and_int_arg(op_sym.to_s + "load receiver and arg")
|
|
|
|
builder.reduce_int( op_sym.to_s + " fix me", me )
|
|
|
|
builder.reduce_int( op_sym.to_s + " fix arg", other )
|
|
|
|
builder.add_code Risc.op( op_sym.to_s + " operator", op_sym , me , other)
|
|
|
|
builder.add_new_int(op_sym.to_s + " new int", me , other)
|
|
|
|
builder.add_reg_to_slot( op_sym.to_s + "save ret" , other , :message , :return_value)
|
2018-04-01 13:56:01 +02:00
|
|
|
compiler.add_mom( Mom::ReturnSequence.new)
|
2018-03-30 16:09:02 +02:00
|
|
|
return compiler.method
|
|
|
|
end
|
2018-03-24 15:51:26 +01:00
|
|
|
def div10( context )
|
2018-04-01 13:50:13 +02:00
|
|
|
s = "div_10 "
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Integer,:div10 ,{})
|
2018-06-29 12:27:57 +02:00
|
|
|
builder = compiler.compiler_builder(compiler.method)
|
2018-04-01 14:13:12 +02:00
|
|
|
#FIX: this could load receiver once, reduce and then transfer twice
|
2018-04-08 17:52:17 +02:00
|
|
|
me = builder.add_known( :receiver )
|
|
|
|
tmp = builder.add_known( :receiver )
|
|
|
|
q = builder.add_known( :receiver )
|
|
|
|
builder.reduce_int( s , me )
|
|
|
|
builder.reduce_int( s , tmp )
|
|
|
|
builder.reduce_int( s , q )
|
2018-04-01 13:50:13 +02:00
|
|
|
const = compiler.use_reg :fixnum , 1
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 1 , const )
|
2015-11-21 13:20:25 +01:00
|
|
|
# int tmp = self >> 1
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# int q = self >> 2
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 2 , const)
|
|
|
|
builder.add_code Risc.op( s , :>> , q , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q >> 4
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 4 , const)
|
|
|
|
builder.add_transfer( s, q , tmp)
|
|
|
|
builder.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q >> 8
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 8 , const)
|
|
|
|
builder.add_transfer( s, q , tmp)
|
|
|
|
builder.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q >> 16
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 16 , const)
|
|
|
|
builder.add_transfer( s, q , tmp)
|
|
|
|
builder.add_code Risc.op( s , :>> , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q + tmp
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.op( s , :+ , q , tmp )
|
2015-11-21 13:20:25 +01:00
|
|
|
# q = q >> 3
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 3 , const)
|
|
|
|
builder.add_code Risc.op( s , :>> , q , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = q * 10
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 10 , const)
|
|
|
|
builder.add_transfer( s, q , tmp)
|
|
|
|
builder.add_code Risc.op( s , :* , tmp , const)
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = self - tmp
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.op( s , :- , me , tmp )
|
|
|
|
builder.add_transfer( s , me , tmp)
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = tmp + 6
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 6 , const)
|
|
|
|
builder.add_code Risc.op( s , :+ , tmp , const )
|
2015-11-21 13:20:25 +01:00
|
|
|
# tmp = tmp >> 4
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_load_data( s , 4 , const)
|
|
|
|
builder.add_code Risc.op( s , :>> , tmp , const )
|
2015-11-21 13:20:25 +01:00
|
|
|
# return q + tmp
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_code Risc.op( s , :+ , q , tmp )
|
2018-04-01 13:01:17 +02:00
|
|
|
|
2018-04-08 17:52:17 +02:00
|
|
|
builder.add_new_int(s,q , tmp)
|
|
|
|
builder.add_reg_to_slot( s , tmp , :message , :return_value)
|
2018-04-01 13:01:17 +02:00
|
|
|
|
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
|