rubyx/lib/vool/statements/send_statement.rb

109 lines
4.1 KiB
Ruby
Raw Normal View History

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
attr_reader :name , :receiver , :arguments
def initialize(name , receiver , arguments )
@name , @receiver , @arguments = name , receiver , arguments
@arguments ||= []
2018-03-10 14:17:36 +01:00
@dynamic = nil
2017-04-02 17:25:30 +02:00
end
2017-04-08 11:10:42 +02:00
def collect(arr)
@receiver.collect(arr)
@arguments.each do |arg|
arg.collect(arr)
end
2017-04-08 11:10:42 +02:00
super
end
# 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
#
# FIXME: we now presume direct (assignable) values for the arguments and receiver.
# in a not so distant future, temporary variables will have to be created
# and complex statements hoisted to assign to them. pps: same as in conditions
def to_mom( in_method )
2017-09-10 21:54:56 +02:00
if(@receiver.ct_type)
simple_call(in_method)
2017-09-10 21:54:56 +02:00
else
cached_call(in_method)
2017-09-10 21:54:56 +02:00
end
end
def message_setup(in_method)
setup = [Mom::MessageSetup.new(in_method)]
receiver = @receiver.slot_class.new([:message , :next_message , :receiver] , @receiver)
2017-09-10 21:54:56 +02:00
arg_target = [:message , :next_message , :arguments]
args = []
@arguments.each_with_index do |arg , index|
args << arg.slot_class.new( arg_target + [index] , arg)
end
setup << Mom::ArgumentTransfer.new( receiver , args )
end
def simple_call(in_method)
type = @receiver.ct_type
called_method = type.resolve_method(@name)
raise "No method #{@name} for #{type}" unless called_method
Mom::Statements.new( message_setup(in_method) << Mom::SimpleCall.new( called_method) )
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)
Mom::Statements.new( cache_check(in_method) + call_cached_method(in_method) )
end
2017-09-14 15:07:02 +02:00
def flatten
raise "flat"
end
2017-12-05 20:46:37 +01:00
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)
2017-09-14 17:42:01 +02:00
# if cached_type != current_type
# cached_type = current_type
# cached_method = current_type.resolve_method(method.name)
2018-03-10 14:17:36 +01:00
if_true = [*build_type_cache_update , *build_method_cache_update(in_method)]
2017-12-05 20:46:37 +01:00
#@if_true.to_mom( in_method ) #find and assign
[Mom::IfStatement.new( build_condition , if_true )]
2017-09-14 15:07:02 +02:00
end
# this may look like a simple_call, but the difference is that we don't know
# the method until run-time. Alas the setup is the same
def call_cached_method(in_method)
2018-03-10 14:17:36 +01:00
@dynamic = Mom::DynamicCall.new()
message_setup(in_method) << @dynamic
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
def build_condition
2018-03-10 14:17:36 +01:00
cached_type = Mom::SlotDefinition.new(@dynamic , [:cached_type])
2017-09-14 17:42:01 +02:00
current_type = Mom::SlotDefinition.new(:message , [:self , :type])
Mom::NotSameCheck.new(cached_type , current_type)
end
def build_type_cache_update
2018-03-10 14:17:36 +01:00
[Mom::SlotMove.new([@dynamic, :cached_type] , [:self , :type])]
2017-09-14 17:42:01 +02:00
end
2018-03-10 14:17:36 +01:00
def build_method_cache_update(in_method)
receiver = StringStatement.new(@name)
resolve = SendStatement.new(:resolve_method , receiver , [SelfStatement.new])
move_method = Mom::SlotMove.new([@dynamic, :cached_method] , [:self , :return])
resolve.to_mom(in_method) << move_method
2017-09-14 17:42:01 +02:00
end
2017-04-01 20:28:57 +02:00
end
end