fix most of mom from calling changes

This commit is contained in:
Torsten Rüger 2019-08-22 22:56:44 +03:00
parent 0c49612e5e
commit 017e7e2971
17 changed files with 106 additions and 84 deletions

View File

@ -27,12 +27,12 @@ module Mom
# For blocks the options are args or frame
# or then the methods arg or frame
def slot_type_for(name)
if @callable.arguments_type.variable_index(name)
slot_def = [:arguments]
if index = @callable.arguments_type.variable_index(name)
return ["arg#{index}".to_sym]
elsif @callable.frame_type.variable_index(name)
slot_def = [:frame]
elsif @method.arguments_type.variable_index(name)
slot_def = [:caller , :caller ,:arguments ]
elsif index = @method.arguments_type.variable_index(name)
return [:caller , :caller , "arg#{index}".to_sym]
elsif @method.frame_type.variable_index(name)
slot_def = [:caller ,:caller , :frame ]
elsif

View File

@ -37,6 +37,7 @@ module Mom
# delegates to SlotLoad for receiver and to the actual args.to_risc
def to_risc(compiler)
transfer = SlotLoad.new(self.source ,[:message , :next_message , :receiver] , @receiver, self).to_risc(compiler)
#TODO transfer the Number of arguments to :arguments_given (to be checked on entry)
compiler.reset_regs
@arguments.each do |arg|
arg.to_risc(compiler)

View File

@ -26,8 +26,7 @@ module Mom
return_address! << return_label
next_message[:return_address] << return_address
block_reg! << message[:arguments]
block_reg << block_reg[index]
block_reg! << message["arg#{index}".to_sym]
message << message[:next_message]
add_code Risc::DynamicJump.new("block_yield", block_reg )

View File

@ -45,8 +45,7 @@ module Mom
end
when Integer
builder.build do
arguments! << message[:arguments]
callable! << arguments[ from ]
callable! << message[ "arg#{from}".to_sym ]
end
else
raise "unknown source #{method_source.class}:#{method_source}"

View File

@ -13,6 +13,7 @@ module Mom
def initialize(left, right , label)
super(label)
raise right.to_s if right.to_s.include?("arguments")
@left , @right = left , right
end

View File

@ -68,12 +68,10 @@ module Mom
# determine how given name need to be accsessed.
# For methods the options are args or frame
def slot_type_for(name)
if @callable.arguments_type.variable_index(name)
type = :arguments
else
type = :frame
if index = @callable.arguments_type.variable_index(name)
return ["arg#{index}".to_sym]
end
[type , name]
[:frame , name]
end
def add_block_compiler(compiler)

View File

@ -25,7 +25,7 @@ module Risc
def test_load_args_from_message
produced = produce_body
assert_equal :r0 , produced.next.array.symbol , produced.next.to_rxf[0..200]
assert_equal 8 , produced.next.index , produced.next.to_rxf[0..200]
assert_equal 3 , produced.next.index , produced.next.to_rxf[0..200]
end
end

View File

@ -7,8 +7,9 @@ module Risc
def setup
super
@input = "local = arg; return local"
@expect = [SlotToReg, SlotToReg, SlotToReg, RegToSlot, SlotToReg, #4
SlotToReg, RegToSlot, Branch] #9
@expect =[ SlotToReg, SlotToReg, RegToSlot, SlotToReg, SlotToReg, #4
RegToSlot, Branch] #9
end
def test_local_assign_instructions
assert_nil msg = check_nil , msg
@ -21,12 +22,12 @@ module Risc
def test_load_args_from_message
produced = produce_body
assert_equal :r0 , produced.array.symbol , produced.next.to_rxf[0..200]
assert_equal 8 , produced.index , produced.next.to_rxf[0..200]
assert_equal 9 , produced.index , produced.next.to_rxf[0..200]
end
def test_load_frame_from_message
produced = produce_body
assert_equal :r0 , produced.next(2).array.symbol , produced.next.to_rxf[0..200]
assert_equal 1 , produced.next.index , produced.next.to_rxf[0..200]
assert_equal :r3 , produced.next(2).array.symbol , produced.next.to_rxf[0..200]
assert_equal 3 , produced.next.index , produced.next.to_rxf[0..200]
end
end
end

View File

@ -10,8 +10,8 @@ module Risc
@expect = [LoadConstant, SlotToReg, RegToSlot, LoadConstant, LoadConstant, #4
SlotToReg, SlotToReg, RegToSlot, RegToSlot, RegToSlot, #9
RegToSlot, SlotToReg, SlotToReg, RegToSlot, LoadConstant, #14
SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg, #19
RegToSlot, SlotToReg, FunctionCall, Label] #24
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot, #19
SlotToReg, FunctionCall, Label] #24
end
def test_send_instructions
@ -37,20 +37,20 @@ module Risc
assert_load( produced , Parfait::Block)
assert_equal :main_block , produced.constant.name
end
def test_load_return
def ttest_load_return
produced = produce_body.next(18)
assert_load( produced , Label)
assert produced.constant.name.start_with?("continue_")
end
def test_function_call
produced = produce_body.next(22)
produced = produce_body.next(21)
assert_equal FunctionCall , produced.class
assert_equal :main , produced.method.name
end
def test_check_continue
produced = produce_body.next(23)
assert_equal Label , produced.class
assert produced.name.start_with?("continue_")
# assert produced.name.start_with?("continue_") , produced.name
end
end
end

View File

@ -10,9 +10,9 @@ module Risc
[ Label ]
end
def postamble
[Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant ,
[Label, SlotToReg, SlotToReg, RegToSlot, LoadConstant,
SlotToReg, RegToSlot, RegToSlot, SlotToReg, SlotToReg,
SlotToReg , FunctionReturn, Label]
SlotToReg, FunctionReturn, Label]
end
def produce_body
produced = produce_main

View File

@ -7,11 +7,11 @@ module Risc
def setup
super
@input = "5.get_internal_word(1)"
@expect = [LoadConstant, LoadConstant, SlotToReg, SlotToReg, RegToSlot,
RegToSlot, RegToSlot, RegToSlot, LoadConstant, SlotToReg,
RegToSlot, LoadConstant, SlotToReg, SlotToReg, RegToSlot,
LoadConstant, SlotToReg, RegToSlot, SlotToReg, FunctionCall,
Label, SlotToReg, RegToSlot, Branch]
@expect = [LoadConstant, LoadConstant, SlotToReg, SlotToReg, RegToSlot, #4
RegToSlot, RegToSlot, RegToSlot, LoadConstant, SlotToReg, #9
RegToSlot, LoadConstant, SlotToReg, RegToSlot, LoadConstant, #14
SlotToReg, RegToSlot, SlotToReg, FunctionCall, Label, #19
SlotToReg, RegToSlot, Branch] #24
end
def test_send_instructions
@ -34,40 +34,36 @@ module Risc
produced = produce_body.next(base+1)
assert_slot_to_reg( produced ,:r0 ,1 , :r2 )
end
def test_load_args
produced = produce_body.next(base+2)
assert_slot_to_reg( produced ,:r2 ,8 , :r2 )
end
def test_store_arg_at
produced = produce_body.next(base+3)
assert_reg_to_slot( produced ,:r1 ,:r2 , 1 )
produced = produce_body.next(base+2)
assert_reg_to_slot( produced ,:r1 ,:r2 , 9 )
end
def test_load_label
produced = produce_body.next(base+4)
produced = produce_body.next(base+3)
assert_load( produced , Label )
end
def test_load_some
produced = produce_body.next(base+5)
produced = produce_body.next(base+4)
assert_slot_to_reg( produced ,:r0 ,1 , :r2 )
end
def test_store_
produced = produce_body.next(base+6)
produced = produce_body.next(base+5)
assert_reg_to_slot( produced ,:r1 ,:r2 , 4 )
end
def test_swap_messages
produced = produce_body.next(base+7)
produced = produce_body.next(base+6)
assert_slot_to_reg( produced ,:r0 ,1 , :r0 )
end
def test_function_call
produced = produce_body
assert_equal FunctionCall , produced.next(base+8).class
assert_equal :get_internal_word , produced.next(base+8).method.name
assert_equal FunctionCall , produced.next(base+7).class
assert_equal :get_internal_word , produced.next(base+7).method.name
end
def test_check_continue
produced = produce_body
assert produced.next(base+9).name.start_with?("continue_")
assert produced.next(base+8).name.start_with?("continue_")
end
end
end

View File

@ -17,40 +17,40 @@ module Risc
def test_send_instructions
assert_nil msg = check_nil , msg
end
def test_load_method
def pest_load_method
method = @produced
assert_load( method, Parfait::CallableMethod ,:r1)
assert_equal :div4 , method.constant.name
end
def test_load_space
def pest_load_space
space = @produced.next(1)
assert_load( space , Parfait::Factory , :r2 )
end
def test_load_first_message #from space (ie r2)
def pest_load_first_message #from space (ie r2)
sl = @produced.next( 2 )
assert_slot_to_reg( sl , :r2 , 2 , :r3 )
end
def test_get_next_next #reduce onto itself
def pest_get_next_next #reduce onto itself
sl = @produced.next( 3 )
assert_slot_to_reg( sl , :r3 , 1 , :r4 )
end
def test_store_next_next_in_space
def pest_store_next_next_in_space
sl = @produced.next( 4 )
assert_reg_to_slot( sl , :r4 , :r2 , 2 )
end
def test_store_message_in_current
def pest_store_message_in_current
sl = @produced.next( 5 )
assert_reg_to_slot( sl , :r3 , :r0 , 1 )
end
def test_store_caller_in_message
def pest_store_caller_in_message
sl = @produced.next( 6 )
assert_reg_to_slot( sl , :r0 , :r3 , 6 )
end
def test_store_method_in_message
def pest_store_method_in_message
sl = @produced.next( 7 )
assert_reg_to_slot( sl , :r1 , :r3 , 7 )
end
def test_label
def pest_label
sl = @produced.next( 20 )
assert_equal Risc::Label , sl.class
assert_equal "return_label" , sl.name

View File

@ -1,7 +1,7 @@
require_relative "helper"
module Mom
class TestBlockCompiler < MiniTest::Test
class TestBlockCompiler1 < MiniTest::Test
include ScopeHelper
def setup
@ -9,6 +9,25 @@ module Mom
@risc = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_risc(code)
end
def test_collection
assert_equal Risc::RiscCollection, @risc.class
end
def test_main_compiler
assert_equal :main , @risc.method_compilers.first.callable.name
end
def test_main_block_compiler
assert_equal :main , @risc.method_compilers.first.block_compilers.first.in_method.name
assert_equal :main_block , @risc.method_compilers.first.block_compilers.first.callable.name
end
end
class TestBlockCompiler2 < MiniTest::Test
include ScopeHelper
def setup
code = as_test_main_block("return arg" , "arg = 1")
@risc = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_risc(code)
end
def test_collection
assert_equal Risc::RiscCollection, @risc.class
end

View File

@ -7,11 +7,10 @@ module Risc
def setup
super
@input = "if(@a) ; arg = 5 ; else; arg = 6; end;return"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero,
LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant,
SlotToReg, RegToSlot, Branch, Label, LoadConstant,
SlotToReg, RegToSlot, Label, LoadConstant, #34
RegToSlot, Branch]
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero, #4
LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant, #9
RegToSlot, Branch, Label, LoadConstant, RegToSlot, #14
Label, LoadConstant, RegToSlot, Branch] #19
end
def test_if_instructions
@ -24,7 +23,9 @@ module Risc
end
def test_false_check
produced = produce_body
assert_equal produced.next(13) , produced.next(4).label
assert_equal IsZero , produced.next(7).class
assert_equal Label , produced.next(12).class
assert_equal produced.next(12).name , produced.next(7).label.name
end
def test_nil_load
produced = produce_body
@ -32,22 +33,27 @@ module Risc
end
def test_nil_check
produced = produce_body
assert_equal produced.next(13) , produced.next(7).label
assert_equal IsZero , produced.next(4).class
assert_equal Label , produced.next(12).class
assert_equal produced.next(12).name , produced.next(4).label.name
end
def test_true_label
produced = produce_body
assert_equal Label , produced.next(8).class
assert produced.next(8).name.start_with?("true_label")
end
def test_merge_label
produced = produce_body
assert produced.next(17).name.start_with?("merge_label")
assert_equal Label , produced.next(15).class
assert produced.next(15).name.start_with?("merge_label")
end
def test_true_jump # should jumpp to merge label
produced = produce_body
assert produced.next(12).label.name.start_with?("merge_label")
assert_equal Branch , produced.next(11).class
assert produced.next(11).label.name.start_with?("merge_label")
end
end
end

View File

@ -7,10 +7,10 @@ module Risc
def setup
super
@input = "if(@a) ; arg = 5 ; end;return"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero,
LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant,
SlotToReg, RegToSlot, Label, LoadConstant, #34
RegToSlot, Branch]
@expect = [Label, SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, #4
IsZero, LoadConstant, OperatorInstruction, IsZero, LoadConstant, #9
RegToSlot, Branch, Label, LoadConstant, RegToSlot, #14
Branch] #19
end
def test_if_instructions
@ -28,10 +28,11 @@ module Risc
end
def test_false_label
produced = produce_body
assert_equal Label , produced.next(12).class
assert_equal Label , produced.next(11).class
end
def test_false_check
produced = produce_body
assert_equal IsZero , produced.next(12).class
assert_equal produced.next(12) , produced.next(4).label
end
def test_nil_load

View File

@ -9,8 +9,8 @@ module Risc
@input = "unless(@a) ; arg = 5 ; end;return"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero, #4
LoadConstant, OperatorInstruction, IsZero, Label, Branch, #9
Label, LoadConstant, SlotToReg, RegToSlot, Label, LoadConstant, #34
RegToSlot, Branch] #14
Label, LoadConstant, RegToSlot, Label, LoadConstant, #14
RegToSlot, Branch] #19
end
def test_if_instructions
@ -50,7 +50,8 @@ module Risc
end
def test_merge_label
produced = produce_body
assert produced.next(14).name.start_with?("merge_label")
assert_equal Label , produced.next(13).class
assert produced.next(13).name.start_with?("merge_label")
end
end

View File

@ -7,14 +7,14 @@ module Risc
def setup
super
@input = "while(5 > 0) ; @a = true; end;return"
@expect = [Label, LoadConstant, LoadConstant, SlotToReg, SlotToReg, #4
@expect = [Label, LoadConstant, LoadConstant, SlotToReg, SlotToReg, #4
RegToSlot, RegToSlot, RegToSlot, RegToSlot, LoadConstant, #9
SlotToReg, RegToSlot, LoadConstant, SlotToReg, SlotToReg, #14
RegToSlot, LoadConstant, SlotToReg, RegToSlot, SlotToReg, #19
FunctionCall, Label, SlotToReg, LoadConstant, OperatorInstruction, #24
IsZero, LoadConstant, OperatorInstruction, IsZero, LoadConstant, #29
SlotToReg, RegToSlot, Branch, Label, LoadConstant, #34
RegToSlot, Branch] #34
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot, #14
LoadConstant, SlotToReg, RegToSlot, SlotToReg, FunctionCall, #19
Label, SlotToReg, LoadConstant, OperatorInstruction, IsZero, #24
LoadConstant, OperatorInstruction, IsZero, LoadConstant, SlotToReg, #29
RegToSlot, Branch, Label, LoadConstant, RegToSlot, #34
Branch] #39
end
def test_while_instructions
@ -39,19 +39,19 @@ module Risc
end
def test_false_check
produced = produce_body
assert_equal Risc::IsZero , produced.next(25).class
assert produced.next(25).label.name.start_with?("merge_label") , produced.next(25).label.name
assert_equal Risc::IsZero , produced.next(24).class
assert produced.next(24).label.name.start_with?("merge_label") , produced.next(24).label.name
end
def test_nil_load
produced = produce_body
assert_equal Risc::LoadConstant , produced.next(29).class
assert_equal Parfait::TrueClass , produced.next(29).constant.class
assert_equal Risc::LoadConstant , produced.next(28).class
assert_equal Parfait::TrueClass , produced.next(28).constant.class
end
def test_back_jump # should jump back to condition label
produced = produce_body
assert_equal Risc::Branch , produced.next(32).class
assert_equal produced.name , produced.next(32).label.name
assert_equal Risc::Branch , produced.next(31).class
assert_equal produced.name , produced.next(31).label.name
end
end