2019-08-12 12:16:15 +02:00
|
|
|
require_relative "div4"
|
|
|
|
require_relative "div10"
|
|
|
|
require_relative "operator"
|
|
|
|
require_relative "comparison"
|
|
|
|
|
2019-08-12 11:36:32 +02:00
|
|
|
module Mom
|
2015-06-29 20:03:58 +02:00
|
|
|
module Builtin
|
2018-08-14 10:23:19 +02:00
|
|
|
# integer related kernel functions
|
2018-11-21 19:29:22 +01:00
|
|
|
# all these functions (return the function they implement) assume interger input
|
|
|
|
# Also the returned integer object has to be passed in to avoid having to allocate it.
|
2018-08-14 10:23:19 +02:00
|
|
|
#
|
2018-11-21 19:29:22 +01:00
|
|
|
# This means the methods will have to be renamed at some point and wrapped
|
2015-06-29 20:03:58 +02:00
|
|
|
module Integer
|
|
|
|
module ClassMethods
|
2018-03-18 17:38:35 +01:00
|
|
|
include CompileHelper
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2018-11-21 19:29:22 +01:00
|
|
|
# div by 4, ie shift right by 2
|
2018-08-14 10:23:19 +02:00
|
|
|
# Mostly created for testing at this point, as it is short
|
|
|
|
# return new int with result
|
2018-04-19 09:00:55 +02:00
|
|
|
def div4(context)
|
|
|
|
compiler = compiler_for(:Integer,:div4 ,{})
|
2019-08-12 10:08:09 +02:00
|
|
|
compiler.add_code Div4.new("div4")
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2015-08-05 17:49:37 +02:00
|
|
|
end
|
2019-08-12 10:08:09 +02:00
|
|
|
|
2018-08-14 10:23:19 +02:00
|
|
|
# implemented by the comparison
|
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-08-14 10:23:19 +02:00
|
|
|
# implemented by the comparison
|
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-08-14 10:23:19 +02:00
|
|
|
# implemented by the comparison
|
2018-04-24 18:45:58 +02:00
|
|
|
def <=( context )
|
|
|
|
comparison( :<= )
|
|
|
|
end
|
2018-08-14 10:23:19 +02:00
|
|
|
# implemented by the comparison
|
2018-04-24 18:45:58 +02:00
|
|
|
def >=( context )
|
|
|
|
comparison( :>= )
|
|
|
|
end
|
2018-08-14 10:23:19 +02:00
|
|
|
|
|
|
|
# all (four) comparison operation are quite similar and implemented here
|
|
|
|
# - reduce the ints (assume int as input)
|
|
|
|
# - subtract the fixnums
|
|
|
|
# - check for minus ( < and > )
|
|
|
|
# - also check for zero (<= and >=)
|
|
|
|
# - load true or false object into return, depending on check
|
|
|
|
# - return
|
2018-04-23 18:39:16 +02:00
|
|
|
def comparison( operator )
|
2018-11-21 19:29:22 +01:00
|
|
|
compiler = compiler_for(:Integer, operator ,{other: :Integer })
|
2019-08-12 10:08:09 +02:00
|
|
|
compiler.add_code Comparison.new("comparison" , operator)
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2018-04-19 21:41:40 +02:00
|
|
|
end
|
2018-08-14 10:23:19 +02:00
|
|
|
|
|
|
|
# implemented all known binary operators that map straight to machine codes
|
|
|
|
# this function (similar to comparison):
|
|
|
|
# - unpacks the intergers to fixnum
|
|
|
|
# - applies the operator (at a risc level)
|
|
|
|
# - gets a new integer and stores the result
|
|
|
|
# - returns the new int
|
2018-04-19 21:13:52 +02:00
|
|
|
def operator_method( op_sym )
|
2018-11-21 19:29:22 +01:00
|
|
|
compiler = compiler_for(:Integer, op_sym ,{other: :Integer })
|
2019-08-12 12:16:15 +02:00
|
|
|
compiler.add_code Operator.new("operator" , op_sym)
|
2018-06-30 22:16:17 +02:00
|
|
|
return compiler
|
2018-03-30 16:09:02 +02:00
|
|
|
end
|
2018-08-09 20:10:05 +02:00
|
|
|
|
2018-08-15 17:18:21 +02:00
|
|
|
# as the name suggests, this devides the integer (self) by ten
|
|
|
|
#
|
|
|
|
# This version is lifted from some arm assembler tricks and is _much_
|
|
|
|
# faster than the general div versions. I think it was about three
|
|
|
|
# times less instructions. Useful for itos
|
|
|
|
#
|
|
|
|
# In fact it is possible to generate specific div function for any given
|
|
|
|
# integer and some are even more faster (as eg div4).
|
2018-03-24 15:51:26 +01:00
|
|
|
def div10( context )
|
2018-03-18 17:38:35 +01:00
|
|
|
compiler = compiler_for(:Integer,:div10 ,{})
|
2019-08-12 10:08:09 +02:00
|
|
|
compiler.add_code Div10.new("div10")
|
|
|
|
return compiler
|
|
|
|
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
|