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 "basic_values"
|
||||||
require_relative "simple_call"
|
require_relative "simple_call"
|
||||||
require_relative "dynamic_call"
|
require_relative "dynamic_call"
|
||||||
|
require_relative "block_yield"
|
||||||
require_relative "resolve_method"
|
require_relative "resolve_method"
|
||||||
require_relative "truth_check"
|
require_relative "truth_check"
|
||||||
require_relative "not_same_check"
|
require_relative "not_same_check"
|
||||||
|
@ -42,8 +42,13 @@ module Mom
|
|||||||
cache_entry << from
|
cache_entry << from
|
||||||
callable_method << cache_entry[:cached_method]
|
callable_method << cache_entry[:cached_method]
|
||||||
end
|
end
|
||||||
|
when Integer
|
||||||
|
builder.build do
|
||||||
|
arguments << message[:arguments]
|
||||||
|
callable_method << arguments[ from ]
|
||||||
|
end
|
||||||
else
|
else
|
||||||
raise "unknown source #{method_source}"
|
raise "unknown source #{method_source.class}:#{method_source}"
|
||||||
end
|
end
|
||||||
build_message_data(builder)
|
build_message_data(builder)
|
||||||
return builder.built
|
return builder.built
|
||||||
|
@ -55,6 +55,7 @@ module Risc
|
|||||||
name = :word if name == :name
|
name = :word if name == :name
|
||||||
name = :message if name == :next_message
|
name = :message if name == :next_message
|
||||||
name = :message if name == :caller
|
name = :message if name == :caller
|
||||||
|
name = :named_list if name == :arguments
|
||||||
sym = name.to_s.camelise.to_sym
|
sym = name.to_s.camelise.to_sym
|
||||||
clazz = Parfait.object_space.get_class_by_name(sym)
|
clazz = Parfait.object_space.get_class_by_name(sym)
|
||||||
raise "Not implemented/found object #{name}:#{sym}" unless clazz
|
raise "Not implemented/found object #{name}:#{sym}" unless clazz
|
||||||
|
@ -44,6 +44,14 @@ module Risc
|
|||||||
attr_reader :register
|
attr_reader :register
|
||||||
end
|
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
|
class IsZero < Branch
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ module Vool
|
|||||||
def make_arg_type( )
|
def make_arg_type( )
|
||||||
type_hash = {}
|
type_hash = {}
|
||||||
@args.each {|arg| type_hash[arg] = :Object }
|
@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 )
|
Parfait::NamedList.type_for( type_hash )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ module Vool
|
|||||||
def to_s
|
def to_s
|
||||||
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
||||||
end
|
end
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
block.call(self)
|
block.call(self)
|
||||||
block.call(@receiver)
|
block.call(@receiver)
|
||||||
@ -21,27 +22,50 @@ module Vool
|
|||||||
end
|
end
|
||||||
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
|
# 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
|
# a varaible. It is (off course) the return value of the message
|
||||||
def slot_definition(compiler)
|
def slot_definition(compiler)
|
||||||
Mom::SlotDefinition.new(:message ,[ :return_value])
|
Mom::SlotDefinition.new(:message ,[ :return_value])
|
||||||
end
|
end
|
||||||
|
|
||||||
def message_setup(in_method,called_method)
|
# A Send breaks down to 2 steps:
|
||||||
setup = Mom::MessageSetup.new( called_method )
|
# - 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)
|
mom_receive = @receiver.slot_definition(compiler)
|
||||||
arg_target = [:message , :next_message , :arguments]
|
arg_target = [:message , :next_message , :arguments]
|
||||||
args = []
|
args = []
|
||||||
@ -49,13 +73,12 @@ module Vool
|
|||||||
args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(compiler))
|
args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(compiler))
|
||||||
end
|
end
|
||||||
setup << Mom::ArgumentTransfer.new( mom_receive , args )
|
setup << Mom::ArgumentTransfer.new( mom_receive , args )
|
||||||
|
setup << Mom::BlockYield.new( arg_index )
|
||||||
end
|
end
|
||||||
|
|
||||||
def simple_call(in_method)
|
def to_s(depth = 0)
|
||||||
type = @receiver.ct_type
|
sen = "#{receiver}.#{name}(#{@arguments.collect{|a| a.to_s}.join(', ')})"
|
||||||
called_method = type.resolve_method(@name)
|
at_depth(depth , sen)
|
||||||
raise "No method #{@name} for #{type}" unless called_method
|
|
||||||
message_setup(in_method,called_method) << Mom::SimpleCall.new(called_method)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -178,7 +178,7 @@ module Parfait
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class TestMethods #< ParfaitTest
|
class TestMethods < ParfaitTest
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
Risc::Builtin.boot_functions
|
Risc::Builtin.boot_functions
|
||||||
|
@ -26,7 +26,7 @@ module Risc
|
|||||||
assert @branch.precheck_called
|
assert @branch.precheck_called
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class TestBranchListenerPositioned #< MiniTest::Test
|
class TestBranchListenerPositioned < MiniTest::Test
|
||||||
def setup
|
def setup
|
||||||
@machine = Risc.machine.boot
|
@machine = Risc.machine.boot
|
||||||
@machine.translate(:interpreter)
|
@machine.translate(:interpreter)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
|
|
||||||
module Vool
|
module Vool
|
||||||
class TestYieldArgsSendMom #< MiniTest::Test
|
class TestYieldArgsSendMom < MiniTest::Test
|
||||||
include MomCompile
|
include MomCompile
|
||||||
include Mom
|
include Mom
|
||||||
|
|
||||||
@ -11,23 +11,49 @@ module Vool
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_array
|
def test_array
|
||||||
check_array [MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad, MessageSetup ,
|
check_array [NotSameCheck, Label, MessageSetup, ArgumentTransfer, BlockYield] ,
|
||||||
ArgumentTransfer, SimpleCall, SlotLoad] , @ins
|
@ins
|
||||||
end
|
end
|
||||||
|
def test_check_label
|
||||||
def pest_one_call
|
assert_equal NotSameCheck, @ins.class
|
||||||
assert_equal SimpleCall, @ins.next(2).class
|
assert @ins.false_jump.name.start_with?("method_ok_")
|
||||||
assert_equal :+, @ins.next(2).method.name
|
|
||||||
end
|
end
|
||||||
def pest_two_call
|
def test_check_left
|
||||||
assert_equal SimpleCall, @ins.next(6).class
|
assert_equal SlotDefinition, @ins.left.class
|
||||||
assert_equal :main, @ins.next(6).method.name
|
assert_equal Parfait::CallableMethod, @ins.left.known_object.class
|
||||||
|
assert_equal :main, @ins.left.known_object.name
|
||||||
|
assert @ins.left.slots.empty?
|
||||||
end
|
end
|
||||||
def pest_args_one_l
|
def test_check_right
|
||||||
left = @ins.next(1).arguments[0].left
|
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 Symbol, left.known_object.class
|
||||||
assert_equal :message, left.known_object
|
assert_equal :message, left.known_object
|
||||||
assert_equal [:next_message, :arguments, 1], left.slots
|
assert_equal [:next_message, :arguments, 1], left.slots
|
||||||
end
|
end
|
||||||
|
def test_yield
|
||||||
|
assert_equal BlockYield, @ins.next(4).class
|
||||||
|
assert_equal 3, @ins.next(4).arg_index
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user