From cff622629704b1ba92415b1fd9079ae548957a60 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 19 Mar 2018 21:19:26 +0530 Subject: [PATCH] own file for check --- lib/mom/instruction/check.rb | 21 +++++++++++++++++++++ lib/mom/instruction/truth_check.rb | 27 +++++++-------------------- 2 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 lib/mom/instruction/check.rb diff --git a/lib/mom/instruction/check.rb b/lib/mom/instruction/check.rb new file mode 100644 index 00000000..447c4f4f --- /dev/null +++ b/lib/mom/instruction/check.rb @@ -0,0 +1,21 @@ +module Mom + + # A base class for conditions in MOM + # Checks (if in code, compare in assm) jump or not, depending + # The logic we choose is closer to the code logic (the asm is reversed) + # When we write an if, the true is the next code, so the Check logic is + # that if the check passes, no jump happens + # This means you need to pass the false label, where to jump to if the + # check does not pass + # Note: In assembler a branch on 0 does just that, it branches if the condition + # is met. This means that the asm implementation is somewhat the reverse + # of the Mom names. But it's easier to understand (imho) + class Check < Instruction + attr_reader :false_jump + def initialize(false_jump) + @false_jump = false_jump + raise "Jump target must be a label #{false_jump}" unless false_jump.is_a?(Label) + end + end + +end diff --git a/lib/mom/instruction/truth_check.rb b/lib/mom/instruction/truth_check.rb index f9a1c010..e3e81f6b 100644 --- a/lib/mom/instruction/truth_check.rb +++ b/lib/mom/instruction/truth_check.rb @@ -1,24 +1,6 @@ module Mom - # A base class for conditions in MOM - # Checks (if in code, compare in assm) jump or not, depending - # The logic we choose is closer to the code logic (the asm is reversed) - # When we write an if, the true is the next code, so the Check logic is - # that if the check passes, no jump happens - # This means you need to pass the false label, where to jump to if the - # check does not pass - # Note: In assembler a branch on 0 does just that, it branches if the condition - # is met. This means that the asm implementation is somewhat the reverse - # of the Mom names. But it's easier to understand (imho) - class Check < Instruction - attr_reader :false_jump - def initialize(false_jump) - @false_jump = false_jump - raise "Jump target must be a label #{false_jump}" unless false_jump.is_a?(Label) - end - end - - # The funny thing about the ruby truth is that is is anything but false or nil + # The funny thing about the ruby truth is that it is anything but false or nil # # To implement the normal ruby logic, we check for false or nil and jump # to the false branch. true_block follows implicitly @@ -33,7 +15,12 @@ module Mom end def to_risc(compiler) - Risc::Label.new(self,"TruthCheck") + false_label = @false_jump.to_risc(compiler) + left = @condition.to_register(compiler,self) + false_load = SlotDefinition.new( FalseConstant.new , nil ).to_register(compiler,self) + left << false_load + left << Risc::IsSame.new(self , left.register , false_load.register , false_label) + left end end