From f3ee11fca513a8015a8984520c8749fb599703a3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 21 Jun 2015 17:25:27 +0300 Subject: [PATCH] simple set optimisation the price of having simple code (generation) is that it is sometimes stupid code that gets generated Filter some of the really daft stuff out --- lib/virtual/passes/set_optimisation.rb | 19 +++++++++++++++++++ test/virtual/test_hello.rb | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 lib/virtual/passes/set_optimisation.rb diff --git a/lib/virtual/passes/set_optimisation.rb b/lib/virtual/passes/set_optimisation.rb new file mode 100644 index 00000000..1fcfe34d --- /dev/null +++ b/lib/virtual/passes/set_optimisation.rb @@ -0,0 +1,19 @@ +module Virtual + + class SetOptimisation + def run block + block.codes.dup.each_with_index do |code , i| + next unless code.is_a? Virtual::Set + next_code = block.codes[i+1] + next unless next_code.is_a? Virtual::Set + next unless code.to == next_code.from + # TODO: a correct implementation would have to check that the code.to + # is not used in further blocks, before being assigned to + new_code = Virtual::Set.new(code.from , next_code.to ) + block.replace(code , [new_code] ) + block.replace(next_code , [] ) + end + end + end + Virtual.machine.add_pass "Virtual::SetOptimisation" +end diff --git a/test/virtual/test_hello.rb b/test/virtual/test_hello.rb index 6deccc63..db7e5bc0 100644 --- a/test/virtual/test_hello.rb +++ b/test/virtual/test_hello.rb @@ -6,7 +6,7 @@ class HelloTest < MiniTest::Test def check machine = Virtual::Machine.boot expressions = machine.compile_main @string_input - output_at = Virtual::Machine::FIRST_PASS + output_at = "Virtual::SetOptimisation" #{}"Register::CallImplementation" machine.run_before output_at puts Sof.write(machine.space)