From deaa06206291549075791e31e93f8613e12fa9b2 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 30 May 2015 14:49:47 +0300 Subject: [PATCH] start to minimize filter unused methods Filter works, need to remove still --- lib/virtual.rb | 1 + lib/virtual/machine.rb | 1 + lib/virtual/passes/minimizer.rb | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 lib/virtual/passes/minimizer.rb diff --git a/lib/virtual.rb b/lib/virtual.rb index 6796e466..98c0bac3 100644 --- a/lib/virtual.rb +++ b/lib/virtual.rb @@ -7,6 +7,7 @@ require "virtual/compiled_method_info" require "virtual/slots/slot" require "virtual/type" # the passes _are_ order dependant +require "virtual/passes/minimizer" require "virtual/passes/send_implementation" require "virtual/passes/get_implementation" require "virtual/passes/enter_implementation" diff --git a/lib/virtual/machine.rb b/lib/virtual/machine.rb index 6e51b65f..43615b32 100644 --- a/lib/virtual/machine.rb +++ b/lib/virtual/machine.rb @@ -42,6 +42,7 @@ module Virtual attr_reader :message , :passes , :space , :class_mappings , :init def run_passes + Minimizer.new.run @passes.each do |pass_class| blocks = [@init] @space.classes.values.each do |c| diff --git a/lib/virtual/passes/minimizer.rb b/lib/virtual/passes/minimizer.rb new file mode 100644 index 00000000..31a190c6 --- /dev/null +++ b/lib/virtual/passes/minimizer.rb @@ -0,0 +1,44 @@ +module Virtual + + # Remove all functions that are not called + # Not called is approximated by the fact that the method name doesn't show up + # in any function reachable from main + class Minimizer + def run + @gonners = [] + Parfait::Space.object_space.classes.values.each do |c| + c.instance_methods.each do |f| + @gonners << f + end + end + init= Parfait::Space.object_space.get_class_by_name("Kernel").get_instance_method "__init__" + remove init + dump_remaining + end + + def remove function + index = @gonners.index function + unless index + puts "function was already removed #{ function.name}" + return + end + @gonners.delete function + function.info.blocks.each do |block| + block.codes.each do |code| + if code.is_a? Virtual::MessageSend + str_name = code.name.to_s + @gonners.each do |stay| + remove stay if(stay.name == str_name) + end + end + remove code.method if code.is_a? Virtual::MethodCall + end + end + end + + def dump_remaining + names = @gonners.collect {|f| f.name } + puts "Dump #{names}" + end + end +end