2019-10-03 23:36:49 +02:00
|
|
|
module Sol
|
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)
|
2018-07-30 13:45:37 +02:00
|
|
|
class SendStatement < CallStatement
|
2017-04-08 11:10:42 +02:00
|
|
|
|
2018-06-28 19:15:24 +02:00
|
|
|
def block
|
|
|
|
return nil if arguments.empty?
|
|
|
|
bl = arguments.last
|
2019-08-19 13:33:02 +02:00
|
|
|
bl.is_a?(LambdaExpression) ? bl : nil
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_block( block )
|
|
|
|
@arguments << block
|
2018-06-26 19:28:27 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:52:56 +01:00
|
|
|
def each(&block)
|
2018-07-30 13:45:37 +02:00
|
|
|
super
|
2018-07-05 13:02:38 +02:00
|
|
|
self.block.each(&block) if self.block
|
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
|
2019-10-03 19:55:41 +02:00
|
|
|
@dynamic ||= SlotMachine::DynamicCall.new()
|
2018-03-18 18:06:36 +01:00
|
|
|
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
|
2019-09-29 11:06:37 +02:00
|
|
|
#
|
|
|
|
# A slight complication occurs for methods defined in superclasses. Since we are
|
|
|
|
# type, not class, based, these are not part of our type.
|
2019-10-03 23:36:49 +02:00
|
|
|
# So we check, and if find, add the source (sol_method) to the class and start
|
|
|
|
# compiling the sol for the receiver_type
|
2019-09-29 11:06:37 +02:00
|
|
|
#
|
2019-10-03 19:55:41 +02:00
|
|
|
def to_slot( compiler )
|
2018-07-16 11:03:40 +02:00
|
|
|
@receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression)
|
2017-09-10 21:54:56 +02:00
|
|
|
if(@receiver.ct_type)
|
2019-09-29 11:06:37 +02:00
|
|
|
method = @receiver.ct_type.get_method(@name)
|
2019-10-01 19:55:05 +02:00
|
|
|
#puts "Known #{@receiver.ct_type}: method #{method}"
|
2019-09-29 11:06:37 +02:00
|
|
|
method = create_method_from_source(compiler) unless( method )
|
2019-08-14 13:59:54 +02:00
|
|
|
return simple_call(compiler, method) if method
|
2017-09-10 21:54:56 +02:00
|
|
|
end
|
2019-08-14 13:59:54 +02:00
|
|
|
cached_call(compiler)
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
2019-09-29 11:06:37 +02:00
|
|
|
# If a method is found in the class (not the type)
|
|
|
|
# we add it to the class that the receiver type represents, and create a compiler
|
2019-10-03 23:36:49 +02:00
|
|
|
# to compile the sol for the specific type (the receiver)
|
2019-09-29 11:06:37 +02:00
|
|
|
def create_method_from_source(compiler)
|
2019-10-03 23:36:49 +02:00
|
|
|
sol_method = @receiver.ct_type.object_class.resolve_method!(@name)
|
|
|
|
return nil unless sol_method
|
|
|
|
#puts "#{sol_method.name} , adding to #{@receiver.ct_type.object_class.name}"
|
|
|
|
@receiver.ct_type.object_class.add_instance_method(sol_method)
|
|
|
|
sol_method.create_callable_method_for(@receiver.ct_type)
|
|
|
|
new_compiler = sol_method.compiler_for(@receiver.ct_type)
|
2019-09-29 11:06:37 +02:00
|
|
|
compiler.add_method_compiler(new_compiler)
|
|
|
|
new_compiler.callable
|
|
|
|
end
|
|
|
|
|
2018-07-05 13:02:38 +02:00
|
|
|
def message_setup(compiler,called_method)
|
2019-10-03 19:55:41 +02:00
|
|
|
setup = SlotMachine::MessageSetup.new( called_method )
|
2020-02-17 08:29:45 +01:00
|
|
|
slot_receive = @receiver.to_slotted(compiler)
|
2020-02-27 17:19:27 +01:00
|
|
|
args = @arguments.collect { |arg| arg.to_slotted(compiler)}
|
2019-10-03 20:07:55 +02:00
|
|
|
setup << SlotMachine::ArgumentTransfer.new(self, slot_receive , args )
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
2019-08-14 13:59:54 +02:00
|
|
|
def simple_call(compiler, called_method)
|
2019-10-03 19:55:41 +02:00
|
|
|
message_setup(compiler,called_method) << SlotMachine::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
|
2018-07-05 13:02:38 +02:00
|
|
|
def cached_call(compiler)
|
|
|
|
cache_check(compiler) << call_cached_method(compiler)
|
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
|
2018-07-05 13:02:38 +02:00
|
|
|
def cache_check(compiler)
|
2019-10-03 19:55:41 +02:00
|
|
|
ok = SlotMachine::Label.new(self,"cache_ok_#{self.object_id}")
|
2018-07-05 13:02:38 +02:00
|
|
|
check = build_condition(ok, compiler) # if cached_type != current_type
|
2019-10-03 19:55:41 +02:00
|
|
|
check << SlotMachine::SlotLoad.new(self,[dynamic_call.cache_entry, :cached_type] , receiver_type_definition(compiler))
|
|
|
|
check << SlotMachine::ResolveMethod.new(self, @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
|
2018-07-05 13:02:38 +02:00
|
|
|
def call_cached_method(compiler)
|
|
|
|
message_setup(compiler,dynamic_call.cache_entry) << dynamic_call
|
2017-09-14 15:07:02 +02:00
|
|
|
end
|
2017-12-05 20:46:37 +01:00
|
|
|
|
2018-07-03 21:18:19 +02:00
|
|
|
def to_s(depth = 0)
|
|
|
|
sen = "#{receiver}.#{name}(#{@arguments.collect{|a| a.to_s}.join(', ')})"
|
|
|
|
at_depth(depth , sen)
|
|
|
|
end
|
|
|
|
|
2017-09-14 17:42:01 +02:00
|
|
|
private
|
2018-07-05 13:02:38 +02:00
|
|
|
def receiver_type_definition(compiler)
|
2020-02-17 08:29:45 +01:00
|
|
|
defi = @receiver.to_slotted(compiler)
|
|
|
|
defi.set_next( SlotMachine::Slot.new(:type) )
|
2018-04-08 21:59:42 +02:00
|
|
|
defi
|
|
|
|
end
|
2020-02-17 08:29:45 +01:00
|
|
|
|
2018-07-05 13:02:38 +02:00
|
|
|
def build_condition(ok_label, compiler)
|
2020-02-15 15:02:03 +01:00
|
|
|
cached_type = SlotMachine::Slotted.for(dynamic_call.cache_entry , [:cached_type])
|
2018-07-05 13:02:38 +02:00
|
|
|
current_type = receiver_type_definition(compiler)
|
2019-10-03 19:55:41 +02:00
|
|
|
SlotMachine::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
|