From fc6aa4e28b1bca03630c95eac6dbc8818003ffb9 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 8 Aug 2018 15:49:47 +0300 Subject: [PATCH] rework operator with builder --- lib/risc/builder.rb | 8 ++++ lib/risc/builtin/integer.rb | 18 ++++--- test/risc/interpreter/calling/test_plus.rb | 56 +++++++--------------- 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index 80a467a8..f8f5daab 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -117,6 +117,14 @@ module Risc add_slot_to_reg( source + "next_i2" , to , int.resolve_index(:next_integer) , int) add_reg_to_slot( source + "store link" , int , space , space_i ) add_reg_to_slot( source + "store value" , from , to , Parfait::Integer.integer_index) + # build do + # space << Parfait.object_space + # to << space[:next_integer] + # integer << to[:next_integer] + # space[:next_integer] << integer + # to[Parfait::Integer.integer_index] << from + # end + end # load receiver and the first argument (int) diff --git a/lib/risc/builtin/integer.rb b/lib/risc/builtin/integer.rb index a4c8d906..6d4f9f58 100644 --- a/lib/risc/builtin/integer.rb +++ b/lib/risc/builtin/integer.rb @@ -9,7 +9,7 @@ module Risc compiler = compiler_for(:Integer,:div4 ,{}) compiler.compiler_builder(compiler.source).build do integer << message[:receiver] - integer << integer[Parfait::Integer.integer_index] + integer.reduce_int integer_reg << 2 integer.op :>> , integer_reg add_new_int("div4", integer , integer_reg) @@ -63,12 +63,16 @@ module Risc def operator_method( op_sym ) compiler = compiler_for(:Integer, op_sym ,{other: :Integer}) builder = compiler.compiler_builder(compiler.source) - me , other = builder.self_and_int_arg(op_sym.to_s + "load receiver and arg") - builder.reduce_int( op_sym.to_s + " fix me", me ) - builder.reduce_int( op_sym.to_s + " fix arg", other ) - builder.add_code Risc.op( op_sym.to_s + " operator", op_sym , me , other) - builder.add_new_int(op_sym.to_s + " new int", me , other) - builder.add_reg_to_slot( op_sym.to_s + "save ret" , other , Risc.message_reg , :return_value) + builder.build do + integer << message[:receiver] + integer_reg << message[:arguments] + integer_reg << integer_reg[ 1] + integer.reduce_int + integer_reg.reduce_int + integer.op op_sym , integer_reg + add_new_int op_sym.to_s , integer , integer_reg + message[:return_value] << integer_reg + end compiler.add_mom( Mom::ReturnSequence.new) return compiler end diff --git a/test/risc/interpreter/calling/test_plus.rb b/test/risc/interpreter/calling/test_plus.rb index 425a6fa8..c88dac8a 100644 --- a/test/risc/interpreter/calling/test_plus.rb +++ b/test/risc/interpreter/calling/test_plus.rb @@ -31,8 +31,7 @@ module Risc end def test_load_5 lod = main_ticks( 19 ) - assert_equal LoadConstant , lod.class - assert_equal Parfait::Integer , lod.constant.class + assert_load( lod , Parfait::Integer , :r1) assert_equal 5 , lod.constant.value end def base @@ -40,35 +39,30 @@ module Risc end def test_slot_receiver #load receiver from message sl = main_ticks( base ) - assert_equal SlotToReg , sl.class - assert_equal :r0 , sl.array.symbol #load from message - assert_equal 2 , sl.index - assert_equal :r1 , sl.register.symbol + assert_slot_to_reg( sl , :r0 , 2 , :r1) end def test_slot_args #load args from message sl = main_ticks( base + 1 ) - assert_equal SlotToReg , sl.class - assert_equal :r0 , sl.array.symbol #load from message - assert_equal 8 , sl.index - assert_equal :r2 , sl.register.symbol + assert_slot_to_reg( sl , :r0 , 8 , :r2) end - def test_slot_arg #load arg 1, destructively from args + def test_slot_arg_int #load arg 1, destructively from args sl = main_ticks( base + 2 ) - assert_equal SlotToReg , sl.class - assert_equal :r2 , sl.array.symbol #load from message - assert_equal 1 , sl.index - assert_equal :r2 , sl.register.symbol + assert_slot_to_reg( sl , :r2 , 1 , :r2) end def test_slot_int1 #load int from object sl = main_ticks( base + 3 ) - assert_equal SlotToReg , sl.class - assert_equal :r1 , sl.array.symbol #load from message - assert_equal 2 , sl.index - assert_equal :r1 , sl.register.symbol + assert_slot_to_reg( sl , :r1 , 2 , :r1) + assert_equal 5 , @interpreter.get_register(:r1) + end + def test_slot_int2 #load int from object + sl = main_ticks( base + 4 ) + assert_slot_to_reg( sl , :r2 , 2 , :r2) + assert_equal 5 , @interpreter.get_register(:r2) end def test_op op = main_ticks(base + 5) assert_equal OperatorInstruction , op.class + assert_equal :+ , op.operator assert_equal :r1 , op.left.symbol assert_equal :r2 , op.right.symbol assert_equal 10 , @interpreter.get_register(:r1) @@ -76,37 +70,21 @@ module Risc end def test_load_int_space cons = main_ticks(base + 6) - assert_equal LoadConstant , cons.class - assert_equal Parfait::Space , cons.constant.class - assert_equal :r3 , cons.register.symbol + assert_load( cons , Parfait::Space , :r3) end def test_load_int_next_space sl = main_ticks(base + 7) - assert_equal SlotToReg , sl.class - assert_equal :r3 , sl.array.symbol #load from space - assert_equal 5 , sl.index - assert_equal :r2 , sl.register.symbol + assert_slot_to_reg( sl , :r3 , 5 , :r2) assert_equal Parfait::Integer , @interpreter.get_register(:r2).class end def test_load_int_next_int sl = main_ticks(base + 8) - assert_equal SlotToReg , sl.class - assert_equal :r2 , sl.array.symbol #load from next_int - assert_equal 1 , sl.index - assert_equal :r4 , sl.register.symbol + assert_slot_to_reg( sl , :r2 , 1 , :r4) assert_equal Parfait::Integer , @interpreter.get_register(:r4).class end def test_load_int_next_int2 sl = main_ticks(base + 9) - assert_equal RegToSlot , sl.class - assert_equal :r3 , sl.array.symbol #store to space - assert_equal 5 , sl.index - assert_equal :r4 , sl.register.symbol - end - def test_sys - sys = main_ticks(75) - assert_equal Syscall , sys.class - assert_equal :exit , sys.name + assert_reg_to_slot( sl , :r4 , :r3 , 5) end end end