2015-10-23 13:22:55 +02:00
|
|
|
module Soml
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-10-09 16:51:14 +02:00
|
|
|
def on_call statement
|
2015-10-16 12:13:56 +02:00
|
|
|
#puts statement
|
2015-11-02 16:32:21 +01:00
|
|
|
name_s , arguments , receiver = *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
|
|
|
|
new_message = Register.resolve_to_register(:new_message)
|
2015-10-28 20:39:59 +01:00
|
|
|
add_code Register.get_slot(statement, :message , :next_message , new_message )
|
2015-09-19 16:57:44 +02:00
|
|
|
if receiver
|
2015-10-26 21:23:06 +01:00
|
|
|
me = process( receiver.first )
|
2015-09-19 16:57:44 +02:00
|
|
|
else
|
2015-10-18 16:20:19 +02:00
|
|
|
me = use_reg @method.for_class.name
|
2015-10-28 20:39:59 +01:00
|
|
|
add_code Register.get_slot(statement, :message , :receiver , me )
|
2015-09-19 16:57:44 +02:00
|
|
|
end
|
2015-10-26 21:23:06 +01:00
|
|
|
if(me.type == :Class)
|
|
|
|
clazz = me.value.meta
|
|
|
|
else
|
|
|
|
# now we have to resolve the method name (+ receiver) into a callable method
|
|
|
|
clazz = Register.machine.space.get_class_by_name(me.type)
|
|
|
|
end
|
2015-10-14 12:48:21 +02:00
|
|
|
# move our receiver there
|
2015-10-23 13:08:12 +02:00
|
|
|
add_code Register.set_slot( statement , me , :new_message , :receiver)
|
2015-11-02 19:11:12 +01:00
|
|
|
|
2015-11-02 16:32:21 +01:00
|
|
|
set_message_details(name_s , arguments)
|
|
|
|
set_arguments(arguments)
|
2015-11-02 19:11:12 +01:00
|
|
|
do_call(clazz , statement)
|
2015-11-02 16:32:21 +01:00
|
|
|
ret = use_reg( :Integer )
|
|
|
|
# the effect of the method is that the NewMessage Return slot will be filled, return it
|
|
|
|
# but move it into a register too
|
|
|
|
add_code Register.get_slot(statement, :message , :return_value , ret )
|
|
|
|
ret
|
|
|
|
end
|
2015-11-02 19:11:12 +01:00
|
|
|
|
2015-11-02 16:32:21 +01:00
|
|
|
private
|
2015-11-02 19:11:12 +01:00
|
|
|
def do_call clazz , statement
|
|
|
|
name = statement.first.first
|
|
|
|
#puts "clazz #{clazz.name}"
|
|
|
|
raise "No such class #{me.type}" unless clazz
|
|
|
|
method = clazz.get_instance_method(name)
|
|
|
|
#puts Register.machine.space.get_class_by_name(:Integer).method_names.to_a
|
|
|
|
raise "Method not implemented #{me.type}.#{name}" unless method
|
|
|
|
ret_tmp = use_reg(:Label)
|
|
|
|
add_code Register::LoadConstant.new(statement.first, method.instructions , ret_tmp)
|
|
|
|
add_code Register.set_slot(statement, ret_tmp , :message , :return_address)
|
|
|
|
Register.issue_call( self , method )
|
|
|
|
end
|
2015-11-02 16:32:21 +01:00
|
|
|
def set_message_details name_s , arguments
|
|
|
|
name = name_s.to_a.first
|
2015-10-14 12:48:21 +02:00
|
|
|
# load method name and set to new message (for exceptions/debug)
|
2015-10-14 15:16:03 +02:00
|
|
|
name_tmp = use_reg(:Word)
|
2015-11-02 16:32:21 +01:00
|
|
|
add_code Register::LoadConstant.new(name_s, name , name_tmp)
|
|
|
|
add_code Register.set_slot( name_s , name_tmp , :new_message , :name)
|
2015-10-27 15:21:11 +01:00
|
|
|
# next arguments. first length then args
|
|
|
|
len_tmp = use_reg(:Integer , arguments.to_a.length )
|
2015-11-02 16:32:21 +01:00
|
|
|
add_code Register::LoadConstant.new(arguments, arguments.to_a.length , len_tmp)
|
|
|
|
add_code Register.set_slot( arguments , len_tmp , :new_message , :indexed_length)
|
|
|
|
end
|
|
|
|
def set_arguments arguments
|
2015-10-27 15:21:11 +01:00
|
|
|
# reset tmp regs for each and load result into new_message
|
2015-09-19 15:28:41 +02:00
|
|
|
arguments.to_a.each_with_index do |arg , i|
|
2015-10-14 12:48:21 +02:00
|
|
|
reset_regs
|
|
|
|
# processing should return the register with the value
|
2015-09-19 17:56:18 +02:00
|
|
|
val = process( arg)
|
2015-10-14 12:48:21 +02:00
|
|
|
raise "Not register #{val}" unless val.is_a?(Register::RegisterValue)
|
2015-10-22 10:32:37 +02:00
|
|
|
# which we load int the new_message at the argument's index (the one comes from c index)
|
2015-10-28 20:39:59 +01:00
|
|
|
set = Register.set_slot( arg , val , :new_message , Parfait::Message.get_indexed(i+1))
|
2015-10-23 13:08:12 +02:00
|
|
|
add_code set
|
2014-08-20 16:14:52 +02:00
|
|
|
end
|
2014-07-13 15:00:48 +02:00
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|