2017-09-14 16:07:02 +03:00
|
|
|
module Mom
|
|
|
|
|
|
|
|
# A dynamic call calls a method at runtime. This off course implies that we don't know the
|
|
|
|
# method at compile time and so must "find" it. Resolving, or finding the method, is a
|
2018-04-08 18:51:20 +03:00
|
|
|
# a seperate instruction though, and here we assume that we know this Method instance.
|
2017-09-14 16:07:02 +03:00
|
|
|
#
|
2018-03-10 18:47:36 +05:30
|
|
|
# Both (to be called) Method instance and the type of receiver are stored as
|
|
|
|
# variables here. The type is used to check before calling.
|
2017-09-14 16:07:02 +03:00
|
|
|
#
|
2018-04-08 18:51:20 +03:00
|
|
|
# Setting up the method is not part of this instructions scope. That setup
|
2018-03-10 18:47:36 +05:30
|
|
|
# includes the type check and any necccessay method resolution.
|
|
|
|
# See vool send statement
|
2017-09-14 16:07:02 +03:00
|
|
|
#
|
|
|
|
class DynamicCall < Instruction
|
2018-03-17 19:03:39 +05:30
|
|
|
attr :cache_entry
|
2017-09-14 16:07:02 +03:00
|
|
|
|
2018-03-10 18:47:36 +05:30
|
|
|
def initialize(type = nil, method = nil)
|
2018-03-17 19:03:39 +05:30
|
|
|
@cache_entry = Parfait::CacheEntry.new(type, method)
|
2017-09-14 16:07:02 +03:00
|
|
|
end
|
2018-03-14 20:25:21 +05:30
|
|
|
|
2018-04-17 20:26:15 +03:00
|
|
|
def to_s
|
|
|
|
str = "DynamicCall "
|
|
|
|
str += cache_entry.cached_method&.name if cache_entry
|
|
|
|
str
|
|
|
|
end
|
|
|
|
|
2018-03-21 11:51:10 +05:30
|
|
|
# One could almost think that one can resolve this to a Risc::FunctionCall
|
|
|
|
# (which btw resolves to a simple jump), alas, the FunctionCall, like all other
|
|
|
|
# jumping, resolves the address at compile time.
|
|
|
|
#
|
|
|
|
# Instead we need a DynamicJump instruction that explicitly takes a register as
|
|
|
|
# a target (not a label)
|
|
|
|
def to_risc(compiler)
|
2018-04-04 20:05:09 +03:00
|
|
|
compiler.add_constant( @cache_entry )
|
2018-03-31 19:17:55 +03:00
|
|
|
reg = compiler.use_reg( :Object )
|
2018-05-29 20:26:00 +03:00
|
|
|
return_label = Risc.label(self, "continue_#{object_id}")
|
2018-04-09 15:06:46 +03:00
|
|
|
save_return = SlotLoad.new([:message,:next_message,:return_address],[return_label],self)
|
|
|
|
moves = save_return.to_risc(compiler)
|
|
|
|
moves << Risc.slot_to_reg(self, :message , :next_message , Risc.message_reg)
|
|
|
|
moves << Risc.load_constant( self , @cache_entry , reg )
|
2018-03-21 11:51:10 +05:30
|
|
|
method_index = Risc.resolve_to_index(:cache_entry , :cached_method)
|
2018-04-09 15:06:46 +03:00
|
|
|
moves << Risc::SlotToReg.new( self , reg ,method_index, reg)
|
|
|
|
moves << Risc::DynamicJump.new(self, reg )
|
|
|
|
moves << return_label
|
2018-03-14 20:25:21 +05:30
|
|
|
end
|
2017-09-14 16:07:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|