From c233bd82d6144561f89e3275bab9888134c88816 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 6 Apr 2018 16:08:35 +0300 Subject: [PATCH] implement [] for RiscValue for the dsl --- lib/mom/instruction/message_setup.rb | 5 ++++- lib/risc/builder.rb | 7 ++----- lib/risc/risc_value.rb | 22 ++++++++++++++++++++++ test/risc/test_risc_value.rb | 7 +++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/mom/instruction/message_setup.rb b/lib/mom/instruction/message_setup.rb index 3c864518..d90be822 100644 --- a/lib/mom/instruction/message_setup.rb +++ b/lib/mom/instruction/message_setup.rb @@ -60,8 +60,11 @@ module Mom def get_message_to( compiler , message) Risc.build(compiler) do space << Parfait.object_space + #message << space[:first_message] + #risc << Risc.slot_to_reg(source + "get next message" , space , :first_message , message) end - + end + def nnop space = compiler.use_reg(:Space) risc = Risc.load_constant("message setup move method" , Parfait.object_space ,space) risc << Risc.slot_to_reg(source + "get next message" , space , :first_message , message) diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index c161e490..07fd3630 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -22,17 +22,14 @@ module Risc return built end def add_instruction(ins) - if(built) - built << ins + if(@built) + @built << ins else @built = ins end end end - class RValue - end - def self.build(compiler, &block) Builder.new(compiler).build( &block ) end diff --git a/lib/risc/risc_value.rb b/lib/risc/risc_value.rb index ab74627e..63812b7b 100644 --- a/lib/risc/risc_value.rb +++ b/lib/risc/risc_value.rb @@ -58,6 +58,12 @@ module Risc @symbol end + # can't overload "=" , so use shift for it. + # move the right side to the left. Left (this) is a RiscValue + # right value may be + # - constant (Parfait object) , resulting in a LoadConstant + # - another RiscValue, resulting in a Transfer instruction + # - an RValue, which gets procuced by the [] below def <<( load ) puts "LOAD #{load}" case load @@ -73,6 +79,22 @@ module Risc builder.add_instruction(ins) if builder return ins end + + # just capture the values in an intermediary object (RValue) + # The RValue then gets used in a RegToSlot ot SlotToReg, where + # the values are unpacked to call Risc.reg_to_slot or Risc.slot_to_reg + def []( index ) + RValue.new( self , index) + end + end + + # Just a struct, see comment for [] of RiscValue + # + class RValue + attr_reader :register , :index + def initialize(register, index) + @register , @index = register , index + end end # The register we use to store the current message object is :r0 diff --git a/test/risc/test_risc_value.rb b/test/risc/test_risc_value.rb index 77e628a9..b9ee809e 100644 --- a/test/risc/test_risc_value.rb +++ b/test/risc/test_risc_value.rb @@ -33,5 +33,12 @@ module Risc @r0 << @r1 assert_equal Transfer , builder.built.class end + def test_index_op + message = @r0[:first_message] + assert_equal RValue , message.class + assert_equal :first_message , message.index + assert_equal @r0 , message.register + end + #message << space[:first_message] end end