[macruby-changes] [1397] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Tue Apr 7 18:24:23 PDT 2009


Revision: 1397
          http://trac.macosforge.org/projects/ruby/changeset/1397
Author:   lsansonetti at apple.com
Date:     2009-04-07 18:24:23 -0700 (Tue, 07 Apr 2009)
Log Message:
-----------
enabled tail-call elimination

Modified Paths:
--------------
    MacRuby/branches/experimental/bench.rb
    MacRuby/branches/experimental/roxor.cpp
    MacRuby/branches/experimental/test_vm/dispatch.rb

Modified: MacRuby/branches/experimental/bench.rb
===================================================================
--- MacRuby/branches/experimental/bench.rb	2009-04-07 23:01:54 UTC (rev 1396)
+++ MacRuby/branches/experimental/bench.rb	2009-04-08 01:24:23 UTC (rev 1397)
@@ -44,6 +44,11 @@
   end
 end
 
+def tail(n)
+  return if n == 0
+  tail(n-1)
+end
+
 class Class1
   def method1; end
   def method2(x); x; end
@@ -122,6 +127,9 @@
     o = Class1.new
     i=0; while i<10000000; o.send(:method1); i+=1; end
   end
+  bm.report('30000000 tail calls') do
+    tail(30000000) 
+  end
 
   # Instance variables.
   bm.report('10000000 ivar read') { bench_ivar_read(10000000) }

Modified: MacRuby/branches/experimental/roxor.cpp
===================================================================
--- MacRuby/branches/experimental/roxor.cpp	2009-04-07 23:01:54 UTC (rev 1396)
+++ MacRuby/branches/experimental/roxor.cpp	2009-04-08 01:24:23 UTC (rev 1397)
@@ -2213,6 +2213,8 @@
     fpm->add(createGVNPass());
     // Simplify the control flow graph (deleting unreachable blocks, etc).
     fpm->add(createCFGSimplificationPass());
+    // Eliminate tail calls.
+    fpm->add(createTailCallEliminationPass());
 }
 
 IMP
@@ -3343,16 +3345,17 @@
 			GlobalVariable *old_class = current_opened_class;
 			current_opened_class = new GlobalVariable(
 				RubyObjTy,
-				false,
+				true,
 				GlobalValue::InternalLinkage,
 				nilVal,
 				"current_opened_class",
 				RoxorCompiler::module);
-			new StoreInst(classVal, current_opened_class, bb);
 
 			std::map<ID, int *> old_ivar_slots_cache = ivar_slots_cache;
 			ivar_slots_cache.clear();
 
+			new StoreInst(classVal, current_opened_class, bb);
+
 			Value *val = compile_node(body->nd_body);
 
 			BasicBlock::InstListType &list = bb->getInstList();
@@ -3457,7 +3460,9 @@
 			params.push_back(compile_node(n->nd_head));
 		    }
 
-		   return cast<Value>(CallInst::Create(f, params.begin(), params.end(), "", bb));
+		    CallInst *inst = CallInst::Create(f, params.begin(), params.end(), "", bb);
+		    inst->setTailCall(true);
+		    return cast<Value>(inst);
 		}
 
 		// Prepare the dispatcher parameters.

Modified: MacRuby/branches/experimental/test_vm/dispatch.rb
===================================================================
--- MacRuby/branches/experimental/test_vm/dispatch.rb	2009-04-07 23:01:54 UTC (rev 1396)
+++ MacRuby/branches/experimental/test_vm/dispatch.rb	2009-04-08 01:24:23 UTC (rev 1397)
@@ -241,3 +241,10 @@
   w = 42
   foo { |x, y = :y| p w }
 }
+
+# Tail-call elimination
+assert '42', %{
+  def foo(n); return if n == 0; foo(n-1); end
+  foo(30000000)
+  p 42
+}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090407/787d651a/attachment-0001.html>


More information about the macruby-changes mailing list