Bug 1076 – by using scope(exit) tail recursion ain't working

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-03-21T11:11:00Z
Last change time
2014-02-16T15:25:54Z
Assigned to
bugzilla
Creator
davidl

Comments

Comment #0 by davidl — 2007-03-21T11:11:08Z
int i; int scp( int n ){ if( n==0 ) return 0; // scope(exit) printf("%d",n); scope(exit) i++; return scp(n-1); } void main() { scp(40000); printf(`%d`,i); } //stack overflow change scope(exit) i++; by i++; then tail recursion works fine
Comment #1 by davidl — 2007-03-21T13:03:28Z
actually i have an idea of how to implement tail recursion with scope . err the idea here is simple, when tail recursion , just loop to the point after SEH structure has been pushed on the stack. coz we are tail recursion the same func, the SEH structure except the next SEH structure part are the same as the SEH structure we have now pushed on the stack. 1. if an exception the current func doesn't handle, then throwing it directly to the caller wouldn't be a problem, coz it would be thrown by the os recursively 2. if an exception in the current func is actually handled, then in the handler, which get called from our OS, would simply do its job, after execution of its handler func , then back to continue tail recursion not return here. it's quite a rough thought. but i think it's possible to have scope() stuff support tail recursion
Comment #2 by bugzilla — 2007-03-21T13:18:55Z
Tail recursion is when the *last* thing a function does is call itself. With scope (exit), this isn't the case, and so the function recurses normally.
Comment #3 by smjg — 2007-03-21T13:37:58Z
(In reply to comment #1) I don't see how that would help. The scope(exit) code would have to be called as many times as there are levels of recursion. In this case, where it doesn't touch any local variables, it could probably be rewritten as a for loop. But in the general case, you'd still need all the stack frames for it to work.
Comment #4 by davidl — 2007-03-22T00:39:49Z
err, yep , i haven't considered the following: void hello( int n) { if (n==0) return; if (n%2==1) { scope(exit) printf(`%d odd`\n,n); hello(n-1); } if (n%2==0) { scope(exit) printf(`%d even`\n,n); hello(n-1); } }