diff --git a/lib/risc/register_value.rb b/lib/risc/register_value.rb index d03d2af9..41328757 100644 --- a/lib/risc/register_value.rb +++ b/lib/risc/register_value.rb @@ -65,10 +65,13 @@ module Risc end # reduce integer to fixnum and add instruction if compiler is used + # TODO: checck type of self, should be integer + # TODO: find a type for the result, maybe fixnum , or data ? + # TODO also check types on reg_to_slot def reduce_int reduce = Risc::SlotToReg.new( "int -> fix" , self , Parfait::Integer.integer_index , self) compiler.add_code(reduce) if compiler - reduce + reduce.register end # when following variables in resolve_and_add, get a new RegisterValue diff --git a/lib/slot_machine/callable_compiler.rb b/lib/slot_machine/callable_compiler.rb index f4dd42c7..5fb0e710 100644 --- a/lib/slot_machine/callable_compiler.rb +++ b/lib/slot_machine/callable_compiler.rb @@ -69,7 +69,6 @@ module SlotMachine while( instruction ) raise "whats this a #{instruction}" unless instruction.is_a?(SlotMachine::Instruction) #puts "adding slot #{instruction.to_s}:#{instruction.next.to_s}" - risc_compiler.reset_regs instruction.to_risc( risc_compiler ) #puts "adding risc #{risc.to_s}:#{risc.next.to_s}" instruction = instruction.next diff --git a/lib/slot_machine/macro/comparison.rb b/lib/slot_machine/macro/comparison.rb index 81dfdb2e..79e2bf33 100644 --- a/lib/slot_machine/macro/comparison.rb +++ b/lib/slot_machine/macro/comparison.rb @@ -3,26 +3,36 @@ module SlotMachine attr_reader :operator def initialize(name , operator) super(name) + #TODO check operator to be in valid range @operator = operator.value end + + # basically use subtract to subtract left from right (or right from left) + # and load true_object in the true branch and false_object in the false + # for the case of =, as in <= or >= we also do not check for zero def to_risc(compiler) builder = compiler.builder(compiler.source) operator = @operator # make accessible in block + false_label = Risc.label("false" , "false") + merge_label = Risc.label("merge" , "merge") + result = Risc::RegisterValue.new(:result , :Object) builder.build do - integer! << message[:receiver] - integer.reduce_int - integer_reg! << message[:arg1] #"other" - integer_reg.reduce_int - swap_names(:integer , :integer_reg) if(operator.to_s.start_with?('<') ) - integer.op :- , integer_reg + left = message[:receiver].to_reg.reduce_int + right = message[:arg1].to_reg.reduce_int + + if(operator.to_s.start_with?('<') ) + right.op :- , left + else + left.op :- , right + end if_minus false_label if_zero( false_label ) if operator.to_s.length == 1 - object! << Parfait.object_space.true_object + add_code Risc::LoadConstant.new(to_s , Parfait.object_space.true_object, result) branch merge_label add_code false_label - object << Parfait.object_space.false_object + add_code Risc::LoadConstant.new(to_s , Parfait.object_space.false_object, result) add_code merge_label - message[:return_value] << object + message[:return_value] << result end return compiler end diff --git a/test/risc/test_register_value1.rb b/test/risc/test_register_value1.rb index 64ed4340..80415264 100644 --- a/test/risc/test_register_value1.rb +++ b/test/risc/test_register_value1.rb @@ -5,9 +5,9 @@ module Risc def setup Parfait.boot!(Parfait.default_test_options) - @r0 = RegisterValue.new(:message , :Message) - @r1 = RegisterValue.new(:id_1234 , :Space) @compiler = Risc.test_compiler + @r0 = RegisterValue.new(:message , :Message).set_compiler(@compiler) + @r1 = RegisterValue.new(:id_1234 , :Space).set_compiler(@compiler) end def test_resolves_index_ok @@ -18,8 +18,10 @@ module Risc end def test_reduce_int ins = @r0.reduce_int - assert_equal SlotToReg , ins.class - assert_equal Parfait::Integer.integer_index , ins.index + assert_equal RegisterValue , ins.class + assert_equal SlotToReg , @compiler.current.class + assert_equal Parfait::Integer.integer_index , @compiler.current.index + assert_equal :message , ins.symbol end def test_get_new_left_0 assert_equal RegisterValue , @r0.get_new_left(:caller , @compiler).class diff --git a/test/slot_machine/macro/test_comparison.rb b/test/slot_machine/macro/test_comparison.rb index f590a3d2..b94361d7 100644 --- a/test/slot_machine/macro/test_comparison.rb +++ b/test/slot_machine/macro/test_comparison.rb @@ -14,22 +14,23 @@ module SlotMachine assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 26 , @method.to_risc.risc_instructions.length + assert_equal 25 , @method.to_risc.risc_instructions.length end + #TODO, check the actual instructions, at least by class end class TestIntComp2Risc < BootTest def setup - @method = get_compiler("Integer",:gt) + @method = get_compiler("Integer",:ge) end def test_slot_length - assert_equal :> , @method.callable.name + assert_equal :>= , @method.callable.name assert_equal 7 , @method.slot_instructions.length end def test_compile assert_equal Risc::MethodCompiler , @method.to_risc.class end def test_risc_length - assert_equal 26 , @method.to_risc.risc_instructions.length + assert_equal 24 , @method.to_risc.risc_instructions.length end end end