start to compile send

still very hacked version of simple call, but a start
This commit is contained in:
Torsten Ruger 2017-04-15 20:58:39 +03:00
parent 265b25d5f4
commit 0d43987005
8 changed files with 130 additions and 43 deletions

View File

@ -6,5 +6,6 @@ module Mom
end end
require_relative "simple_call"
require_relative "slot_load" require_relative "slot_load"
require_relative "return_sequence" require_relative "return_sequence"

18
lib/mom/simple_call.rb Normal file
View File

@ -0,0 +1,18 @@
module Mom
# A SimpleCall is just that, a simple call. This could be called a function call too,
# meaning we managed to resolve the function at compile time and all we have to do is
# actually call it.
#
# As the call setup is done beforehand (for both simple and cached call), the
# calling really means just jumping to the address. Simple.
#
class SimpleCall < Instruction
attr_reader :method
def initialize(method)
@method = method
end
end
end

View File

@ -1,6 +1,6 @@
# A message is what is sent when you invoke a method. Args and stuff are packed up in to a Message # A message is what is created when a message is sent. Args and stuff are packed up in to a
# and the Message is sent to the receiver. # Message and the Message is activated (by swapping it into the machine).
# Part of the housekeeping (see attributes) makes messages a double linked list (next_message and # Part of the housekeeping (see attributes) makes messages a double linked list (next_message and
# caller) , and maybe surprisingly this means that we can create all messages at runtime # caller) , and maybe surprisingly this means that we can create all messages at runtime

View File

@ -167,7 +167,7 @@ module Vool
# this is a call to super without args (z = zero arity) # this is a call to super without args (z = zero arity)
def on_zsuper exp def on_zsuper exp
SendStatement.new( nil , SuperStatement.new ) SendStatement.new( nil , SuperStatement.new , nil)
end end
# this is a call to super with args and # this is a call to super with args and

View File

@ -2,15 +2,71 @@ module Vool
class SendStatement < Statement class SendStatement < Statement
attr_reader :name , :receiver , :arguments attr_reader :name , :receiver , :arguments
def initialize(name , receiver , arguments = []) def initialize(name , receiver , arguments )
@name , @receiver , @arguments = name , receiver , arguments @name , @receiver , @arguments = name , receiver , arguments
@arguments ||= []
end end
def collect(arr) def collect(arr)
@receiver.collect(arr) @receiver.collect(arr)
@arguments.collect(arr) @arguments.each do |arg|
puts "ARG#{arg}"
arg.collect(arr)
end
super super
end end
# Sending in a dynamic language is off course not as simple as just calling.
# The function that needs to be called depends after all on the receiver,
# and no guarantees can be made on what that is.
#
# It helps to know that usually (>99%) the class of the receiver does not change.
# Our stategy then is to cache the functions and only dynamically determine it in
# case of a miss (the 1%, and first invocation)
#
# As cache key we must use the type of the object (which is the first word of _every_ object)
# as that is constant, and function implementations depend on the type (not class)
#
# A Send breaks down to 2 steps:
# - Setting up the next message, with receiver, arguments, and (importantly) return address
# - a CachedCall , or a SimpleCall, depending on weather the receiver type can be determined
#
# FIXME: we now presume direct (assignable) values for the arguments and receiver.
# in a not so distant future, temporary variables will have to be created
# and complex statements hoisted to assign to them. pps: same as in conditions
def to_mom( method )
message_setup + call_instruction
end
def message_setup
pops = [Mom::SlotConstant.new([:message , :next_message , :receiver] , @receiver) ]
@arguments.each_with_index do |arg , index|
arg_target = [:message , :next_message , :arguments]
pops << Mom::SlotConstant.new( arg_target + index , @arg)
end
pops
end
def call_instruction
if(receiver_type)
simple_call
else
cached_call
end
end
def receiver_type
Parfait.object_space.get_class_by_name(:Integer).instance_type
end
def simple_call
type = receiver_type
method = type.get_method(@name)
[Mom::SimpleCall.new( method) ]
end
def cached_call
[Mom::SlotConstant.new([:message , :next_message , :receiver] , @receiver) ]
end
end end
end end

View File

@ -1,38 +0,0 @@
module Risc
# So when an object calls a method, or sends a message, this is what it sends: a Message
# A message contains the sender, return and exceptional return addresses,the arguments,
# and a slot for the named_list.
# As such it is a very run-time object, deep in the machinery as it were, and does not have
# meaningful methods you could call at compile time.
# The methods that are there, are nevertheless meant to be called at compile time and generate
# code, rather than executing it.
# The caller creates the Message and passes control to the receiver's method
# The receiver create a new NamedList to hold local and temporary variables and (later) creates
# default values for arguments that were not passed
# How the actual finding of the method takes place (acording to the ruby rules) is not simple,
# but as there is a guaranteed result (be it method_missing) it does not matter to the passing
# mechanism described
# During compilation Message and named_list objects are created to do type analysis
class Message
def initialize me , normal , exceptional
@me = me
@next_normal = normal
@next_exception = exceptional
@arguments = arguments
# a named_list represents the local and temporary variables at a point in the program.
@named_list = nil
end
attr_reader :me, :next_normal, :next_exception, :arguments , :locals
#
end
end

View File

@ -34,4 +34,17 @@ module Vool
assert_equal IntegerStatement , @stats.first.right.class assert_equal IntegerStatement , @stats.first.right.class
end end
end end
class TestReturnSendMom < MiniTest::Test
include MomCompile
def setup
Risc.machine.boot
@stats = compile_first_method( "return foo").first
end
def test_two_instructions_are_returned
#need to implement send first
# assert_equal 2 , @stats.length
end
end
end end

View File

@ -0,0 +1,37 @@
require_relative "helper"
module Vool
class TestSendMom < MiniTest::Test
include MomCompile
def setup
Risc.machine.boot
@stats = compile_first_method( "5.mod4").first
end
def test_class_compiles
assert_equal Mom::SlotConstant , @stats.first.class , @stats
end
def test_slot_is_set
assert @stats.first.left
end
def test_two_instructions_are_returned
assert_equal 2 , @stats.length
end
def test_receiver_class
assert_equal Mom::SlotConstant, @stats.first.class
end
def test_receiver_move
assert_equal :receiver, @stats.first.left[2]
end
def test_call_is
assert_equal Mom::SimpleCall, @stats[1].class
end
def test_call_has_method
assert_equal Parfait::TypedMethod, @stats[1].method.class
end
def test_call_has_right_method
assert_equal :mod4, @stats[1].method.name
end
end
end