rubyx/lib/vm/method_compiler/call_site.rb

100 lines
3.7 KiB
Ruby
Raw Normal View History

2017-01-14 18:28:44 +01:00
module Vm
2016-12-09 11:22:37 +01:00
module CallSite
2016-12-09 11:22:37 +01:00
def on_CallSite( statement )
raise "not inside method " unless @method
reset_regs
#move the new message (that we need to populate to make a call) to std register
2017-01-16 16:44:34 +01:00
load_new_message(statement)
2016-12-09 11:22:37 +01:00
me = get_me( statement )
2016-12-14 12:24:42 +01:00
type = get_my_type(me)
method = type.get_method(statement.name)
2017-01-16 16:44:34 +01:00
raise "Method not implemented for me:#{me} #{type.inspect}.#{statement.name}" unless method
# move our receiver there
add_reg_to_slot( statement , me , :new_message , :receiver)
2015-11-02 19:11:12 +01:00
set_message_details(method , statement , statement.arguments)
set_arguments(method , statement.arguments)
2017-01-16 16:44:34 +01:00
ret = use_reg( :Object ) #FIXME real return type
Register.issue_call( self , method )
2015-11-02 16:32:21 +01:00
# the effect of the method is that the NewMessage Return slot will be filled, return it
# but move it into a register too
add_slot_to_reg(statement, :new_message , :return_value , ret )
2015-11-02 16:32:21 +01:00
ret
end
2015-11-02 19:11:12 +01:00
2015-11-02 16:32:21 +01:00
private
2016-12-09 11:22:37 +01:00
2017-01-16 16:44:34 +01:00
def load_new_message(statement)
new_message = Register.resolve_to_register(:new_message)
add_slot_to_reg(statement, :message , :next_message , new_message )
new_message
end
2016-12-09 11:22:37 +01:00
def get_me( statement )
if statement.receiver
me = process( statement.receiver )
else
2016-12-14 12:24:42 +01:00
me = use_reg @method.for_type
add_slot_to_reg(statement, :message , :receiver , me )
2016-12-09 11:22:37 +01:00
end
me
end
2017-01-16 16:44:34 +01:00
2016-12-14 12:24:42 +01:00
def get_my_type( me )
# now we have to resolve the method name (+ receiver) into a callable method
2016-12-14 12:24:42 +01:00
case me.type
when Parfait::Type
type = me.type
when Symbol
type = Parfait.object_space.get_class_by_name(me.type).instance_type
2016-12-14 12:24:42 +01:00
else
raise me.inspect
2016-12-09 11:22:37 +01:00
end
2016-12-14 12:24:42 +01:00
raise "Not type #{type}" unless type.is_a? Parfait::Type
type
2016-12-09 11:22:37 +01:00
end
# load method name and set to new message (for exceptions/debug)
def set_message_details( method , name_s , arguments )
name = name_s.name
name_tmp = use_reg(:Word)
add_load_constant("#{name} load method name", name , name_tmp)
add_reg_to_slot( "#{name} store method name" , name_tmp , :new_message , :name)
2017-01-03 21:56:00 +01:00
# next arg type
args_reg = use_reg(:Type , method.arguments )
list_reg = use_reg(:NamedList , arguments )
add_load_constant("#{name} load arguments type", method.arguments , args_reg)
add_slot_to_reg( "#{name} get args from method" , :new_message , :arguments , list_reg )
add_reg_to_slot( "#{name} store args type in args" , args_reg , list_reg , 1 )
2015-11-02 16:32:21 +01:00
end
def set_arguments( method , arguments )
# reset tmp regs for each and load result into new_message
arg_type = method.arguments
message = "Arg number mismatch, method=#{arg_type.instance_length - 1} , call=#{arguments.length}"
raise message if (arg_type.instance_length - 1 ) != arguments.length
arguments.each_with_index do |arg , i |
2016-12-28 20:10:14 +01:00
store_arg_no(arguments , arg_type , arg , i + 1) #+1 for ruby(0 based)
end
2014-07-13 15:00:48 +02:00
end
2016-12-28 20:10:14 +01:00
def store_arg_no(arguments , arg_type , arg , i )
reset_regs
i = i + 1 # disregarding type field
val = process( arg) # processing should return the register with the value
raise "Not register #{val}" unless val.is_a?(Register::RegisterValue)
#FIXME definately needs some tests
raise "TypeMismatch calling with #{val.type} , instead of #{arg_type.type_at(i)}" if val.type != arg_type.type_at(i)
list_reg = use_reg(:NamedList , arguments )
add_slot_to_reg( "Set arg #{i}:#{arg}" , :new_message , :arguments , list_reg )
# which we load int the new_message at the argument's index
add_reg_to_slot( arg , val , list_reg , i ) #one for type and one for ruby
end
end
end