rubyx/lib/slot_machine/instructions/dynamic_call.rb

49 lines
1.7 KiB
Ruby
Raw Normal View History

2019-10-03 20:07:55 +02:00
module SlotMachine
2017-09-14 15:07:02 +02:00
# 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
# a seperate instruction though, and here we assume that we know this Method instance.
2017-09-14 15:07:02 +02:00
#
2018-03-10 14:17:36 +01:00
# 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 15:07:02 +02:00
#
# Setting up the method is not part of this instructions scope. That setup
2018-03-10 14:17:36 +01:00
# includes the type check and any necccessay method resolution.
# See sol send statement
2017-09-14 15:07:02 +02:00
#
class DynamicCall < Instruction
2018-03-17 14:33:39 +01:00
attr :cache_entry
2017-09-14 15:07:02 +02:00
2018-03-10 14:17:36 +01:00
def initialize(type = nil, method = nil)
2018-03-17 14:33:39 +01:00
@cache_entry = Parfait::CacheEntry.new(type, method)
2017-09-14 15:07:02 +02:00
end
2018-03-14 15:55:21 +01:00
2018-04-17 19:26:15 +02:00
def to_s
str = "DynamicCall "
str += cache_entry.cached_method&.name if cache_entry and cache_entry.cached_method
2018-04-17 19:26:15 +02:00
str
end
# 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)
entry = compiler.load_object(@cache_entry)[:cached_method].to_reg
compiler.add_constant( @cache_entry)
return_label = Risc.label(self, "continue_#{object_id}")
return_address = compiler.load_object(return_label)
compiler.build(to_s) do
message[:next_message][:return_address] << return_address
message << message[:next_message]
add_code Risc::DynamicJump.new("DynamicCall", entry )
add_code return_label
end
2018-03-14 15:55:21 +01:00
end
2017-09-14 15:07:02 +02:00
end
end