2017-01-14 18:28:44 +01:00
|
|
|
module Vm
|
2016-12-09 11:22:37 +01:00
|
|
|
module CallSite
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2016-12-09 11:22:37 +01:00
|
|
|
def on_CallSite( statement )
|
2015-10-05 23:27:13 +02:00
|
|
|
raise "not inside method " unless @method
|
2015-10-14 12:48:21 +02:00
|
|
|
reset_regs
|
2015-10-18 16:20:19 +02:00
|
|
|
#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)
|
2016-12-27 19:37:53 +01:00
|
|
|
|
2016-12-30 12:33:07 +01:00
|
|
|
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
|
2016-12-27 19:37:53 +01:00
|
|
|
|
2015-10-14 12:48:21 +02:00
|
|
|
# move our receiver there
|
2016-12-28 19:37:54 +01:00
|
|
|
add_reg_to_slot( statement , me , :new_message , :receiver)
|
2015-11-02 19:11:12 +01:00
|
|
|
|
2016-12-27 19:37:53 +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
|
2016-12-27 19:37:53 +01:00
|
|
|
|
2017-01-19 08:02:29 +01:00
|
|
|
Risc.issue_call( self , method )
|
2016-12-27 19:37:53 +01:00
|
|
|
|
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
|
2016-12-28 19:37:54 +01:00
|
|
|
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)
|
2017-01-19 08:02:29 +01:00
|
|
|
new_message = Risc.resolve_to_register(:new_message)
|
2017-01-16 16:44:34 +01:00
|
|
|
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
|
2016-12-28 19:37:54 +01:00
|
|
|
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 )
|
2016-12-21 21:35:36 +01:00
|
|
|
# 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
|
2016-12-30 13:10:49 +01:00
|
|
|
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
|
2016-12-21 21:35:36 +01:00
|
|
|
|
|
|
|
# load method name and set to new message (for exceptions/debug)
|
2016-12-27 19:37:53 +01:00
|
|
|
def set_message_details( method , name_s , arguments )
|
2016-03-07 10:55:28 +01:00
|
|
|
name = name_s.name
|
2015-10-14 15:16:03 +02:00
|
|
|
name_tmp = use_reg(:Word)
|
2016-12-28 19:37:54 +01:00
|
|
|
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
|
2016-12-27 19:37:53 +01:00
|
|
|
args_reg = use_reg(:Type , method.arguments )
|
2016-12-21 21:35:36 +01:00
|
|
|
list_reg = use_reg(:NamedList , arguments )
|
2017-01-04 20:34:51 +01:00
|
|
|
add_load_constant("#{name} load arguments type", method.arguments , args_reg)
|
2016-12-28 19:37:54 +01:00
|
|
|
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
|
2016-12-27 19:37:53 +01:00
|
|
|
|
|
|
|
def set_arguments( method , arguments )
|
2015-10-27 15:21:11 +01:00
|
|
|
# reset tmp regs for each and load result into new_message
|
2016-12-27 19:37:53 +01:00
|
|
|
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)
|
2014-08-20 16:14:52 +02:00
|
|
|
end
|
2014-07-13 15:00:48 +02:00
|
|
|
end
|
2016-12-30 12:33:07 +01:00
|
|
|
|
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
|
2017-01-19 08:02:29 +01:00
|
|
|
raise "Not register #{val}" unless val.is_a?(Risc::RiscValue)
|
2016-12-28 20:10:14 +01:00
|
|
|
#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
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|