optimization/6692: Tail call optimization does not work for function pointers

wlux@uni-muenster.de wlux@uni-muenster.de
Fri May 17 04:26:00 GMT 2002


>Number:         6692
>Category:       optimization
>Synopsis:       Tail call optimization does not work for function pointers
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Fri May 17 04:26:05 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Wolfgang Lux
>Release:        gcc version 3.1
>Organization:
>Environment:
Sparc Solaris 2.8
gcc configured with: --program-suffix=-3.1 --enable-languages=c
>Description:
The tail call optimization introduced in Gnu C version 3
does not work if the tail call is made through a function
pointer, e.g.

int exec(int (*f)(int), int n)
{
    return (*f)(n);
}

The code generated for this function calls the function
passed via f recursively in a new stack frame instead of
releasing exec's stack frame and jumping to f's entry-point.
>How-To-Repeat:
Compile bug.c with -O2 and run the executable. It crashes
with a Segmentation Fault due to a stack overflow (can be
confirmed with gdb).
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="bug.c"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="bug.c"

aW50IGYxKGludCksIGYyKGludCk7CQkvKiBmdW5jdGlvbiBwcm90b3R5cGVzICovCmludCBnMShp
bnQpLCBnMihpbnQpOwppbnQgZXhlYyhpbnQgKCopKGludCksIGludCk7CgovKiBkaXJlY3QgY2Fs
bCAqLwppbnQgZjEoaW50IG4pCnsKICAgIHJldHVybiBmMihuICsgMSk7Cn0KCmludCBmMihpbnQg
bikKewogICAgcmV0dXJuIGYxKG4gKyAxKTsKfQoKLyogaW5kaXJlY3QgY2FsbHMgKi8KaW50IGcx
KGludCBuKQp7CiAgICByZXR1cm4gZXhlYyhnMiwgbiArIDEpOwp9CgppbnQgZzIoaW50IG4pCnsK
ICAgIHJldHVybiBleGVjKGcxLCBuICsgMSk7Cn0KCmludCBleGVjKGludCAoKmYpKGludCksIGlu
dCBuKQp7CiAgICByZXR1cm4gKCpmKShuKTsKfQoKCi8qIG1haW4gZnVuY3Rpb24gKi8KaW50IG1h
aW4oKQp7CiNpZmRlZiBJTkZJTklURQogICAgLyogdGhpcyBlbnRlcnMgYW4gaW5maW5pdGUgbG9v
cCAqLwogICAgZjEoMCk7CgojZWxzZQogICAgLyogdGhpcyBjYWxsIGNyYXNoZXMgd2l0aCBhIHN0
YWNrIG92ZXJmbG93ICovCiAgICBnMSgwKTsKI2VuZGlmCn0K



More information about the Gcc-bugs mailing list