2017-04-01 20:28:57 +02:00
|
|
|
module Vool
|
2018-03-10 14:17:36 +01:00
|
|
|
# Sending in a dynamic language is off course not as simple as just calling.
|
|
|
|
# The function that needs to be called depends after all on the receiver,
|
|
|
|
# and no guarantees can be made on what that is.
|
|
|
|
#
|
|
|
|
# It helps to know that usually (>99%) the class of the receiver does not change.
|
|
|
|
# Our stategy then is to cache the functions and only dynamically determine it in
|
|
|
|
# case of a miss (the 1%, and first invocation)
|
|
|
|
#
|
|
|
|
# As cache key we must use the type of the object (which is the first word of _every_ object)
|
|
|
|
# as that is constant, and function implementations depend on the type (not class)
|
2017-04-02 11:59:07 +02:00
|
|
|
class SendStatement < Statement
|
2018-06-26 19:28:27 +02:00
|
|
|
attr_reader :name , :receiver , :arguments , :block
|
2017-04-06 15:06:51 +02:00
|
|
|
|
2017-04-15 19:58:39 +02:00
|
|
|
def initialize(name , receiver , arguments )
|
2017-04-06 15:06:51 +02:00
|
|
|
@name , @receiver , @arguments = name , receiver , arguments
|
2017-04-15 19:58:39 +02:00
|
|
|
@arguments ||= []
|
2017-04-02 17:25:30 +02:00
|
|
|
end
|
2017-04-08 11:10:42 +02:00
|
|
|
|
2018-06-26 19:28:27 +02:00
|
|
|
def block=( block )
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
|
2018-03-15 12:52:56 +01:00
|
|
|
def normalize
|
2018-04-27 08:59:01 +02:00
|
|
|
statements = Statements.new([])
|
|
|
|
arguments = []
|
2018-04-27 20:55:41 +02:00
|
|
|
@arguments.each_with_index do |arg , index |
|
2018-04-27 08:59:01 +02:00
|
|
|
normalize_arg(arg , arguments , statements)
|
|
|
|
end
|
|
|
|
if statements.empty?
|
|
|
|
return SendStatement.new(@name, @receiver , @arguments)
|
|
|
|
else
|
|
|
|
statements << SendStatement.new(@name, @receiver , arguments)
|
|
|
|
return statements
|
2018-03-20 18:01:39 +01:00
|
|
|
end
|
2018-04-27 08:59:01 +02:00
|
|
|
end
|
2018-04-27 20:55:41 +02:00
|
|
|
|
2018-04-27 08:59:01 +02:00
|
|
|
def normalize_arg(arg , arguments , statements)
|
|
|
|
if arg.respond_to?(:slot_definition) and !arg.is_a?(SendStatement)
|
|
|
|
arguments << arg
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assign = LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, arg)
|
|
|
|
statements << assign
|
2018-04-27 20:55:41 +02:00
|
|
|
arguments << LocalVariable.new(assign.name)
|
2018-03-15 12:52:56 +01:00
|
|
|
end
|
|
|
|
|
2018-04-27 08:59:01 +02:00
|
|
|
def to_s
|
|
|
|
"#{receiver}.#{name}(#{arguments.join(',')})"
|
|
|
|
end
|
2018-03-15 12:52:56 +01:00
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
|
|
|
block.call(@receiver)
|
2017-04-15 19:58:39 +02:00
|
|
|
@arguments.each do |arg|
|
2018-03-15 12:52:56 +01:00
|
|
|
block.call(arg)
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
2017-04-08 11:10:42 +02:00
|
|
|
end
|
|
|
|
|
2018-03-18 18:06:36 +01:00
|
|
|
# lazy init this, to keep the dependency (which goes to parfait and booting) at bay
|
|
|
|
def dynamic_call
|
|
|
|
@dynamic ||= Mom::DynamicCall.new()
|
|
|
|
end
|
|
|
|
|
2017-04-15 19:58:39 +02:00
|
|
|
# A Send breaks down to 2 steps:
|
|
|
|
# - Setting up the next message, with receiver, arguments, and (importantly) return address
|
2017-09-14 15:07:02 +02:00
|
|
|
# - a CachedCall , or a SimpleCall, depending on wether the receiver type can be determined
|
2017-09-12 16:49:42 +02:00
|
|
|
def to_mom( in_method )
|
2018-03-16 06:33:29 +01:00
|
|
|
@receiver = SelfExpression.new(in_method.for_type) if @receiver.is_a?(SelfExpression)
|
2017-09-10 21:54:56 +02:00
|
|
|
if(@receiver.ct_type)
|
2017-09-12 16:49:42 +02:00
|
|
|
simple_call(in_method)
|
2017-09-10 21:54:56 +02:00
|
|
|
else
|
2017-09-12 16:49:42 +02:00
|
|
|
cached_call(in_method)
|
2017-09-10 21:54:56 +02:00
|
|
|
end
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
2018-03-16 08:03:11 +01:00
|
|
|
# When used as right hand side, this tells what data to move to get the result into
|
|
|
|
# a varaible. It is (off course) the return value of the message
|
|
|
|
def slot_definition(in_method)
|
|
|
|
Mom::SlotDefinition.new(:message ,[ :return_value])
|
|
|
|
end
|
|
|
|
|
2018-04-05 11:24:49 +02:00
|
|
|
def message_setup(in_method,called_method)
|
|
|
|
setup = Mom::MessageSetup.new( called_method )
|
2018-03-20 18:01:39 +01:00
|
|
|
mom_receive = @receiver.slot_definition(in_method)
|
2017-09-10 21:54:56 +02:00
|
|
|
arg_target = [:message , :next_message , :arguments]
|
2017-09-11 13:22:33 +02:00
|
|
|
args = []
|
2018-05-15 15:25:55 +02:00
|
|
|
@arguments.each_with_index do |arg , index| # +1 because of type
|
|
|
|
args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(in_method))
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
2018-03-20 18:01:39 +01:00
|
|
|
setup << Mom::ArgumentTransfer.new( mom_receive , args )
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
2017-09-12 16:49:42 +02:00
|
|
|
def simple_call(in_method)
|
2017-04-19 19:59:13 +02:00
|
|
|
type = @receiver.ct_type
|
2017-09-12 16:49:42 +02:00
|
|
|
called_method = type.resolve_method(@name)
|
|
|
|
raise "No method #{@name} for #{type}" unless called_method
|
2018-04-05 11:24:49 +02:00
|
|
|
message_setup(in_method,called_method) << Mom::SimpleCall.new(called_method)
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
2017-09-14 15:07:02 +02:00
|
|
|
# this breaks cleanly into two parts:
|
|
|
|
# - check the cached type and if neccessary update
|
|
|
|
# - call the cached method
|
|
|
|
def cached_call(in_method)
|
2018-03-16 06:02:11 +01:00
|
|
|
cache_check(in_method) << call_cached_method(in_method)
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
2017-09-14 15:07:02 +02:00
|
|
|
# check that current type is the cached type
|
|
|
|
# if not, change and find method for the type (simple_call to resolve_method)
|
|
|
|
# conceptually easy in ruby, but we have to compile that "easy" ruby
|
|
|
|
def cache_check(in_method)
|
2018-03-21 17:28:43 +01:00
|
|
|
ok = Mom::Label.new("cache_ok_#{self.object_id}")
|
2018-04-08 21:59:42 +02:00
|
|
|
check = build_condition(ok, in_method) # if cached_type != current_type
|
|
|
|
check << Mom::SlotLoad.new([dynamic_call.cache_entry, :cached_type] , receiver_type_definition(in_method))
|
2018-04-08 17:55:17 +02:00
|
|
|
check << Mom::ResolveMethod.new( @name , dynamic_call.cache_entry )
|
2018-03-21 08:08:28 +01:00
|
|
|
check << ok
|
2017-09-14 15:07:02 +02:00
|
|
|
end
|
|
|
|
|
2018-04-05 11:24:49 +02:00
|
|
|
# to call the method (that we know now to be in the cache), we move the method
|
|
|
|
# to reg1, do the setup (very similar to static) and call
|
2017-09-14 15:07:02 +02:00
|
|
|
def call_cached_method(in_method)
|
2018-04-05 11:24:49 +02:00
|
|
|
message_setup(in_method,dynamic_call.cache_entry) << dynamic_call
|
2017-09-14 15:07:02 +02:00
|
|
|
end
|
2017-12-05 20:46:37 +01:00
|
|
|
|
2017-09-14 17:42:01 +02:00
|
|
|
private
|
2018-04-08 21:59:42 +02:00
|
|
|
def receiver_type_definition(in_method)
|
|
|
|
defi = @receiver.slot_definition(in_method)
|
|
|
|
defi.slots << :type
|
|
|
|
defi
|
|
|
|
end
|
|
|
|
def build_condition(ok_label, in_method)
|
2018-03-18 18:06:36 +01:00
|
|
|
cached_type = Mom::SlotDefinition.new(dynamic_call.cache_entry , [:cached_type])
|
2018-04-08 21:59:42 +02:00
|
|
|
current_type = receiver_type_definition(in_method)
|
2018-03-21 08:08:28 +01:00
|
|
|
Mom::NotSameCheck.new(cached_type , current_type, ok_label)
|
2017-09-14 17:42:01 +02:00
|
|
|
end
|
2017-04-01 20:28:57 +02:00
|
|
|
end
|
|
|
|
end
|