bring the blocks down to mom level
reusing message_setup, but adding yield specific instructions
This commit is contained in:
parent
d80ef4bf4e
commit
f5c284b3a0
31
lib/mom/instruction/block_yield.rb
Normal file
31
lib/mom/instruction/block_yield.rb
Normal file
@ -0,0 +1,31 @@
|
||||
module Mom
|
||||
|
||||
# A BlockYield calls an argument block. All we need to know is the index
|
||||
# of the argument, and the rest is almost as simple as a SimpleCall
|
||||
|
||||
class BlockYield < Instruction
|
||||
attr :arg_index
|
||||
|
||||
def initialize(index)
|
||||
@arg_index = index
|
||||
end
|
||||
|
||||
def to_s
|
||||
"BlockYield[#{arg_index}] "
|
||||
end
|
||||
|
||||
def to_risc(compiler)
|
||||
block = compiler.use_reg( :Block )
|
||||
return_label = Risc.label(self, "continue_#{object_id}")
|
||||
save_return = SlotLoad.new([:message,:next_message,:return_address],[return_label],self)
|
||||
moves = save_return.to_risc(compiler)
|
||||
moves << Risc.slot_to_reg( self , Risc.message_reg ,:arguments , block)
|
||||
moves << Risc.slot_to_reg( self , block ,arg_index , block)
|
||||
|
||||
moves << Risc.slot_to_reg(self, Risc.message_reg , :next_message , Risc.message_reg)
|
||||
moves << Risc::DynamicJump.new(self, block )
|
||||
moves << return_label
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -32,6 +32,7 @@ require_relative "check"
|
||||
require_relative "basic_values"
|
||||
require_relative "simple_call"
|
||||
require_relative "dynamic_call"
|
||||
require_relative "block_yield"
|
||||
require_relative "resolve_method"
|
||||
require_relative "truth_check"
|
||||
require_relative "not_same_check"
|
||||
|
@ -42,8 +42,13 @@ module Mom
|
||||
cache_entry << from
|
||||
callable_method << cache_entry[:cached_method]
|
||||
end
|
||||
when Integer
|
||||
builder.build do
|
||||
arguments << message[:arguments]
|
||||
callable_method << arguments[ from ]
|
||||
end
|
||||
else
|
||||
raise "unknown source #{method_source}"
|
||||
raise "unknown source #{method_source.class}:#{method_source}"
|
||||
end
|
||||
build_message_data(builder)
|
||||
return builder.built
|
||||
|
@ -55,6 +55,7 @@ module Risc
|
||||
name = :word if name == :name
|
||||
name = :message if name == :next_message
|
||||
name = :message if name == :caller
|
||||
name = :named_list if name == :arguments
|
||||
sym = name.to_s.camelise.to_sym
|
||||
clazz = Parfait.object_space.get_class_by_name(sym)
|
||||
raise "Not implemented/found object #{name}:#{sym}" unless clazz
|
||||
|
@ -44,6 +44,14 @@ module Risc
|
||||
attr_reader :register
|
||||
end
|
||||
|
||||
# A Dynamic yield is very much like a DynamicJump, especially in it's idea
|
||||
#
|
||||
# The implentation differes slightly, as we use a chache entry in the DynamicJump
|
||||
# but a block in the DynamicYield.
|
||||
# Using means that we assume the register to be ready loaded with a Block
|
||||
class DynamicYield < DynamicJump
|
||||
end
|
||||
|
||||
class IsZero < Branch
|
||||
end
|
||||
|
||||
|
@ -31,7 +31,7 @@ module Vool
|
||||
def make_arg_type( )
|
||||
type_hash = {}
|
||||
@args.each {|arg| type_hash[arg] = :Object }
|
||||
type_hash[:implicit_block] = :Object if has_yield?
|
||||
type_hash[:implicit_block] = :Block if has_yield?
|
||||
Parfait::NamedList.type_for( type_hash )
|
||||
end
|
||||
|
||||
|
@ -13,6 +13,7 @@ module Vool
|
||||
def to_s
|
||||
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
block.call(self)
|
||||
block.call(@receiver)
|
||||
@ -21,27 +22,50 @@ module Vool
|
||||
end
|
||||
end
|
||||
|
||||
# A Send breaks down to 2 steps:
|
||||
# - Setting up the next message, with receiver, arguments, and (importantly) return address
|
||||
# - a SimpleCall,
|
||||
def to_mom( compiler )
|
||||
@parfait_block = @block.to_mom(compiler) if @block
|
||||
@receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression)
|
||||
if(@receiver.ct_type)
|
||||
simple_call(compiler)
|
||||
else
|
||||
raise "ERROR"
|
||||
end
|
||||
end
|
||||
|
||||
# When used as right hand side, this tells what data to move to get the result into
|
||||
# a varaible. It is (off course) the return value of the message
|
||||
def slot_definition(compiler)
|
||||
Mom::SlotDefinition.new(:message ,[ :return_value])
|
||||
end
|
||||
|
||||
def message_setup(in_method,called_method)
|
||||
setup = Mom::MessageSetup.new( called_method )
|
||||
# A Send breaks down to 2 steps:
|
||||
# - Setting up the next message, with receiver, arguments, and (importantly) return address
|
||||
# - a SimpleCall,
|
||||
def to_mom( compiler )
|
||||
@parfait_block = @block.to_mom(compiler) if @block
|
||||
@receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression)
|
||||
yield_call(compiler)
|
||||
end
|
||||
|
||||
# this breaks into two parts:
|
||||
# - check the calling method and break to a (not implemented) dynamic version
|
||||
# - call the block, that is the last argument of the method
|
||||
def yield_call(compiler)
|
||||
method_check(compiler) << yield_arg_block(compiler)
|
||||
end
|
||||
|
||||
# check that the calling method is the method that the block was created in.
|
||||
# In that case variable resolution is reasy and we can prceed to yield
|
||||
# Note: the else case is not implemented (ie passing lambdas around)
|
||||
# this needs run-time variable resolution, which is just not done.
|
||||
# we brace ourselves with the check, and exit (later raise) if . . .
|
||||
def method_check(compiler)
|
||||
ok_label = Mom::Label.new("method_ok_#{self.object_id}")
|
||||
compile_method = Mom::SlotDefinition.new( compiler.get_method , [])
|
||||
runtime_method = Mom::SlotDefinition.new( :message , [ :method] )
|
||||
check = Mom::NotSameCheck.new(compile_method , runtime_method, ok_label)
|
||||
# TODO? Maybe create mom instructions for this
|
||||
#builder = compiler.code_builder("yield")
|
||||
#Risc::Builtin::Object.exit_sequence(builder)
|
||||
#check << builder.built
|
||||
check << ok_label
|
||||
end
|
||||
|
||||
# to call the block (that we know now to be the last arg),
|
||||
# we do a message setup, arg transfer and the a arg_yield (which is similar to dynamic_call)
|
||||
def yield_arg_block(compiler)
|
||||
arg_index = compiler.get_method.arguments_type.get_length
|
||||
setup = Mom::MessageSetup.new( arg_index )
|
||||
mom_receive = @receiver.slot_definition(compiler)
|
||||
arg_target = [:message , :next_message , :arguments]
|
||||
args = []
|
||||
@ -49,13 +73,12 @@ module Vool
|
||||
args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(compiler))
|
||||
end
|
||||
setup << Mom::ArgumentTransfer.new( mom_receive , args )
|
||||
setup << Mom::BlockYield.new( arg_index )
|
||||
end
|
||||
|
||||
def simple_call(in_method)
|
||||
type = @receiver.ct_type
|
||||
called_method = type.resolve_method(@name)
|
||||
raise "No method #{@name} for #{type}" unless called_method
|
||||
message_setup(in_method,called_method) << Mom::SimpleCall.new(called_method)
|
||||
def to_s(depth = 0)
|
||||
sen = "#{receiver}.#{name}(#{@arguments.collect{|a| a.to_s}.join(', ')})"
|
||||
at_depth(depth , sen)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -178,7 +178,7 @@ module Parfait
|
||||
end
|
||||
end
|
||||
end
|
||||
class TestMethods #< ParfaitTest
|
||||
class TestMethods < ParfaitTest
|
||||
def setup
|
||||
super
|
||||
Risc::Builtin.boot_functions
|
||||
|
@ -26,7 +26,7 @@ module Risc
|
||||
assert @branch.precheck_called
|
||||
end
|
||||
end
|
||||
class TestBranchListenerPositioned #< MiniTest::Test
|
||||
class TestBranchListenerPositioned < MiniTest::Test
|
||||
def setup
|
||||
@machine = Risc.machine.boot
|
||||
@machine.translate(:interpreter)
|
||||
|
@ -1,7 +1,7 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Vool
|
||||
class TestYieldArgsSendMom #< MiniTest::Test
|
||||
class TestYieldArgsSendMom < MiniTest::Test
|
||||
include MomCompile
|
||||
include Mom
|
||||
|
||||
@ -11,23 +11,49 @@ module Vool
|
||||
end
|
||||
|
||||
def test_array
|
||||
check_array [MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad, MessageSetup ,
|
||||
ArgumentTransfer, SimpleCall, SlotLoad] , @ins
|
||||
check_array [NotSameCheck, Label, MessageSetup, ArgumentTransfer, BlockYield] ,
|
||||
@ins
|
||||
end
|
||||
|
||||
def pest_one_call
|
||||
assert_equal SimpleCall, @ins.next(2).class
|
||||
assert_equal :+, @ins.next(2).method.name
|
||||
def test_check_label
|
||||
assert_equal NotSameCheck, @ins.class
|
||||
assert @ins.false_jump.name.start_with?("method_ok_")
|
||||
end
|
||||
def pest_two_call
|
||||
assert_equal SimpleCall, @ins.next(6).class
|
||||
assert_equal :main, @ins.next(6).method.name
|
||||
def test_check_left
|
||||
assert_equal SlotDefinition, @ins.left.class
|
||||
assert_equal Parfait::CallableMethod, @ins.left.known_object.class
|
||||
assert_equal :main, @ins.left.known_object.name
|
||||
assert @ins.left.slots.empty?
|
||||
end
|
||||
def pest_args_one_l
|
||||
left = @ins.next(1).arguments[0].left
|
||||
def test_check_right
|
||||
assert_equal SlotDefinition, @ins.right.class
|
||||
assert_equal :message, @ins.right.known_object
|
||||
assert_equal [:method] , @ins.right.slots
|
||||
end
|
||||
def test_label
|
||||
assert_equal Label, @ins.next(1).class
|
||||
assert @ins.next(1).name.start_with?("method_ok_")
|
||||
end
|
||||
def test_setup
|
||||
assert_equal MessageSetup, @ins.next(2).class
|
||||
assert_equal 3, @ins.next(2).method_source
|
||||
end
|
||||
def test_transfer
|
||||
assert_equal ArgumentTransfer, @ins.next(3).class
|
||||
assert_equal 1, @ins.next(3).arguments.length
|
||||
end
|
||||
def test_receiver
|
||||
assert_equal :message , @ins.next(3).receiver.known_object
|
||||
assert_equal [:receiver] , @ins.next(3).receiver.slots
|
||||
end
|
||||
def test_args_one_l
|
||||
left = @ins.next(3).arguments[0].left
|
||||
assert_equal Symbol, left.known_object.class
|
||||
assert_equal :message, left.known_object
|
||||
assert_equal [:next_message, :arguments, 1], left.slots
|
||||
end
|
||||
def test_yield
|
||||
assert_equal BlockYield, @ins.next(4).class
|
||||
assert_equal 3, @ins.next(4).arg_index
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user