Index: env.h
===================================================================
RCS file: /src/ruby/env.h,v
retrieving revision 1.7
diff -u -r1.7 env.h
--- env.h	2001/02/14 05:51:57	1.7
+++ env.h	2001/04/25 06:32:28
@@ -32,6 +32,9 @@
 
 #define FRAME_ALLOCA 0
 #define FRAME_MALLOC 1
+#ifdef TAIL_RECURSIVE
+#define FRAME_DONT_TAILJUMP 2
+#endif /* TAIL_RECURSIVE */
 
 extern struct SCOPE {
     struct RBasic super;
Index: node.h
===================================================================
RCS file: /src/ruby/node.h,v
retrieving revision 1.21
diff -u -r1.21 node.h
--- node.h	2001/02/14 05:51:57	1.21
+++ node.h	2001/04/25 06:32:28
@@ -58,6 +58,11 @@
     NODE_CALL,
     NODE_FCALL,
     NODE_VCALL,
+#ifdef TAIL_RECURSIVE
+    NODE_TAIL_CALL,
+    NODE_TAIL_FCALL,
+    NODE_TAIL_VCALL,
+#endif /* TAIL_RECURSIVE */
     NODE_SUPER,
     NODE_ZSUPER,
     NODE_ARRAY,
Index: eval.c
===================================================================
RCS file: /src/ruby/eval.c,v
retrieving revision 1.172
diff -u -r1.172 eval.c
--- eval.c	2001/03/28 08:43:24	1.172
+++ eval.c	2001/04/25 06:32:28
@@ -527,6 +527,13 @@
 static struct FRAME *top_frame;
 static struct SCOPE *top_scope;
 
+#ifdef TAIL_RECURSIVE
+static VALUE tail_call_recv = Qnil;
+static ID tail_call_mid = 0;
+static VALUE tail_call_args = Qnil;
+static int tail_call_scope = 0;
+#endif /* TAIL_RECURSIVE */
+
 #define PUSH_FRAME() {			\
     struct FRAME _frame;		\
     _frame.prev = ruby_frame;		\
@@ -608,10 +615,18 @@
        rb_gc_force_recycle((VALUE)tag); \
 } while (0)
 
+#ifdef TAIL_RECURSIVE
+#define POP_BLOCK() 			\
+   POP_BLOCK_TAG(_block.tag);		\
+   ruby_frame->flags |= _block.frame.flags & FRAME_DONT_TAILJUMP; \
+   ruby_block = _block.prev; 		\
+}
+#else /* not TAIL_RECURSIVE */
 #define POP_BLOCK() 			\
    POP_BLOCK_TAG(_block.tag);		\
    ruby_block = _block.prev; 		\
 }
+#endif /* TAIL_RECURSIVE */
 
 struct RVarmap *ruby_dyna_vars;
 #define PUSH_VARS() {			\
@@ -810,6 +825,9 @@
 #define TAG_RAISE	0x6
 #define TAG_THROW	0x7
 #define TAG_FATAL	0x8
+#ifdef TAIL_RECURSIVE
+#define TAG_TAILJUMP	0x9
+#endif
 #define TAG_MASK	0xf
 
 VALUE ruby_class;
@@ -1969,6 +1987,10 @@
 static void return_check _((void));
 #define return_value(v) prot_tag->retval = (v)
 
+#ifdef TAIL_RECURSIVE
+static void optimize_tail_call _((NODE*));
+#endif /* TAIL_RECURSIVE */
+
 static VALUE
 rb_eval(self, n)
     VALUE self;
@@ -2503,6 +2525,68 @@
 	result = rb_call(CLASS_OF(self),self,node->nd_mid,0,0,2);
 	break;
 
+#ifdef TAIL_RECURSIVE
+      case NODE_TAIL_CALL:
+        {
+	  VALUE recv;
+	  int argc; VALUE *argv; /* used in SETUP_ARGS */
+	  TMP_PROTECT;
+
+	  BEGIN_CALLARGS;
+	  recv = rb_eval(self, node->nd_recv);
+	  SETUP_ARGS(node->nd_args);
+	  END_CALLARGS;
+	  if (ruby_frame->flags & FRAME_DONT_TAILJUMP) {
+	      result = rb_call(CLASS_OF(recv),recv,node->nd_mid,argc,argv,0);
+	  }
+	  else {
+	      tail_call_recv = recv;
+	      tail_call_mid = node->nd_mid;
+	      tail_call_args = rb_ary_new4(argc, argv);
+	      tail_call_scope = 0;
+	      return_check();
+	      JUMP_TAG(TAG_TAILJUMP);
+	  }
+        }
+	break;
+
+      case NODE_TAIL_FCALL:
+        {
+	  int argc; VALUE *argv; /* used in SETUP_ARGS */
+	  TMP_PROTECT;
+
+	  BEGIN_CALLARGS;
+	  SETUP_ARGS(node->nd_args);
+	  END_CALLARGS;
+	  if (ruby_frame->flags & FRAME_DONT_TAILJUMP) {
+	      result = rb_call(CLASS_OF(self),self,node->nd_mid,argc,argv,1);
+	  }
+	  else {
+	      tail_call_recv = self;
+	      tail_call_mid = node->nd_mid;
+	      tail_call_args = rb_ary_new4(argc, argv);
+	      tail_call_scope = 1;
+	      return_check();
+	      JUMP_TAG(TAG_TAILJUMP);
+	  }
+        }
+	break;
+
+      case NODE_TAIL_VCALL:
+	if (ruby_frame->flags & FRAME_DONT_TAILJUMP) {
+	    result = rb_call(CLASS_OF(self),self,node->nd_mid,0,0,2);
+	}
+	else {
+	    tail_call_recv = self;
+	    tail_call_mid = node->nd_mid;
+	    tail_call_args = Qnil;
+	    tail_call_scope = 2;
+	    return_check();
+	    JUMP_TAG(TAG_TAILJUMP);
+	}
+	break;
+#endif /* TAIL_RECURSIVE */
+
       case NODE_SUPER:
       case NODE_ZSUPER:
 	{
@@ -2944,6 +3028,9 @@
 	    if (body && origin == ruby_class && body->nd_noex & NOEX_UNDEF) {
 		noex |= NOEX_UNDEF;
 	    }
+#ifdef TAIL_RECURSIVE
+	    optimize_tail_call(node->nd_defn);
+#endif /* TAIL_RECURSIVE */
 	    rb_add_method(ruby_class, node->nd_mid, node->nd_defn, noex);
 	    rb_clear_cache_by_id(node->nd_mid);
 	    if (scope_vmode == SCOPE_MODFUNC) {
@@ -4442,14 +4529,18 @@
     VALUE *argv;		/* OK */
     int scope;
 {
+    VALUE result;
     NODE  *body;		/* OK */
     int    noex;
-    ID     id = mid;
+    ID     id;
     struct cache_entry *ent;
+    int state;
 
     if (!klass) {
 	rb_raise(rb_eNotImpError, "method call on terminated object");
     }
+  again:
+    id = mid;
     /* is it in the method cache? */
     ent = cache + EXPR1(klass, mid);
     if (ent->mid == mid && ent->klass == klass) {
@@ -4483,7 +4574,42 @@
 	}
     }
 
+#ifdef TAIL_RECURSIVE
+    if (nd_type(body) == NODE_CFUNC)
+	return rb_call0(klass, recv, id, argc, argv, body, noex & NOEX_UNDEF);
+
+    PUSH_TAG(PROT_NONE);
+    if ((state = EXEC_TAG()) == 0) {
+	result = rb_call0(klass, recv, id, argc, argv, body, noex & NOEX_UNDEF);
+    }
+    POP_TAG();
+    switch (state) {
+      case 0:
+	return result;
+      case TAG_TAILJUMP:
+	recv = tail_call_recv;
+	klass = CLASS_OF(recv);
+	mid = tail_call_mid;
+	if (NIL_P(tail_call_args)) {
+	    argc = 0;
+	    argv = 0;
+	}
+	else {
+	    argc = RARRAY(tail_call_args)->len;
+	    argv = RARRAY(tail_call_args)->ptr;
+	}
+	scope = tail_call_scope;
+	tail_call_recv = Qnil;
+	tail_call_mid = 0;
+	tail_call_args = Qnil;
+	tail_call_scope = 0;
+	goto again;
+      default:
+	JUMP_TAG(state);
+    }
+#else /* not TAIL_RECURSIVE */
     return rb_call0(klass, recv, id, argc, argv, body, noex & NOEX_UNDEF);
+#endif /* TAIL_RECURSIVE */
 }
 
 VALUE
@@ -5816,6 +5942,11 @@
     rb_global_variable((VALUE*)&ruby_eval_tree);
     rb_global_variable((VALUE*)&ruby_dyna_vars);
 
+#ifdef TAIL_RECURSIVE
+    rb_global_variable(&tail_call_recv);
+    rb_global_variable(&tail_call_args);
+#endif /* TAIL_RECURSIVE */
+
     rb_define_virtual_variable("$@", errat_getter, errat_setter);
     rb_define_hooked_variable("$!", &ruby_errinfo, 0, errinfo_setter);
 
@@ -6166,6 +6297,10 @@
     scope_dup(data->scope);
     proc_save_safe_level(proc);
 
+#ifdef TAIL_RECURSIVE
+    ruby_block->frame.flags |= FRAME_DONT_TAILJUMP;
+#endif /* TAIL_RECURSIVE */
+
     return proc;
 }
 
@@ -8756,3 +8891,142 @@
 	tt = tt->prev;
     }
 }
+
+#ifdef TAIL_RECURSIVE
+static void
+optimize_return(node)
+    NODE *node;
+{
+  again:
+    if (!node) return;
+
+    switch (nd_type(node)) {
+      case NODE_SCOPE:
+	node = node->nd_next;
+	goto again;
+
+      case NODE_BLOCK:
+	while (node->nd_next) {
+	    optimize_return(node->nd_head);
+	    node = node->nd_next;
+	}
+	node = node->nd_head;
+	goto again;
+
+      case NODE_BEGIN:
+	node = node->nd_body;
+	goto again;
+
+      case NODE_NEWLINE:
+	node = node->nd_next;
+	goto again;
+
+      case NODE_IF:
+	optimize_return(node->nd_body);
+	optimize_return(node->nd_else);
+	break;
+
+      case NODE_CASE:
+	node = node->nd_body;
+	while (node) {
+	    if (nd_type(node) != NODE_WHEN)
+		goto again;
+	    optimize_return(node->nd_body);
+	    node = node->nd_next;
+	}
+	break;
+
+      case NODE_WHILE:
+      case NODE_UNTIL:
+      case NODE_FOR:
+	node = node->nd_body;
+	goto again;
+
+      case NODE_RETURN:
+	if (node->nd_stts) {
+	    node = node->nd_stts;
+	    switch (nd_type(node)) {
+	      case NODE_CALL:
+		nd_set_type(node, NODE_TAIL_CALL);
+		break;
+	      case NODE_FCALL:
+		nd_set_type(node, NODE_TAIL_FCALL);
+		break;
+	      case NODE_VCALL:
+		nd_set_type(node, NODE_TAIL_VCALL);
+		break;
+	    }
+	}
+	break;
+
+      case NODE_ITER:		/* maybe catch */
+      case NODE_RESCUE:
+      case NODE_ENSURE:
+      default:
+	break;
+    }
+}
+  
+static void
+optimize_tail_call(node)
+    NODE *node;
+{
+  again:
+    if (!node) return;
+
+    switch (nd_type(node)) {
+      case NODE_SCOPE:
+	node = node->nd_next;
+	goto again;
+
+      case NODE_BLOCK:
+	while (node->nd_next) {
+	    optimize_return(node->nd_head);
+	    node = node->nd_next;
+	}
+	node = node->nd_head;
+	goto again;
+
+      case NODE_BEGIN:
+	node = node->nd_body;
+	goto again;
+
+      case NODE_NEWLINE:
+	node = node->nd_next;
+	goto again;
+
+      case NODE_IF:
+	optimize_tail_call(node->nd_body);
+	optimize_tail_call(node->nd_else);
+	break;
+
+      case NODE_CASE:
+	node = node->nd_body;
+	while (node) {
+	    if (nd_type(node) != NODE_WHEN)
+		goto again;
+	    optimize_tail_call(node->nd_body);
+	    node = node->nd_next;
+	}
+	break;
+
+      case NODE_CALL:
+	nd_set_type(node, NODE_TAIL_CALL);
+	break;
+
+      case NODE_FCALL:
+	nd_set_type(node, NODE_TAIL_FCALL);
+	break;
+
+      case NODE_VCALL:
+	nd_set_type(node, NODE_TAIL_VCALL);
+	break;
+
+      case NODE_RESCUE:
+      case NODE_ENSURE:
+      default:
+	optimize_return(node);
+	break;
+    }
+}
+#endif /* TAIL_RECURSIVE */
